2

I had used for loop to iterate nested objects, I am trying to replace forEach with the map function, without success. Can anyone help me with this?

schema.js

const products_schema = {

    product_name: {
        auto: false,
        type: "string",
        min: 5,
        max: 10,
        special_characters: ['_', ' '],
        numbers: true,
        alphabet: true,
        required: true,
        correct: ""
    },
    product_image: {
        auto: false,
        type: "array:string",
        min: 0,
        max: 50,
        required: true
    }
}

const specification_schema = {
    brand: {
        auto: false,
        type: "string",
        min: 10,
        max: 50,
        special_characters: ['_', ' '],
        numbers: true,
        alphabet: true,
        required: true
    }
}

let schema = {
    products_schema:products_schema,
    specification_schema:specification_schema
}
for(var key in schema)
{
    var value = schema[key]
    Object.keys(value).forEach(key => console.log(value[key].type));
}

"Expected output:"
string
array:string
string

8
  • What's wrong with for loop? Commented Jun 4, 2019 at 10:18
  • 1
    Object.keys(schema).map(key=> { var value = schema[key] Object.keys(value).forEach(key => console.log(value[key].type)); }) Commented Jun 4, 2019 at 10:19
  • for loop works fine. But my requirement is to replace for with map Commented Jun 4, 2019 at 10:20
  • What is the expected value of schema? Commented Jun 4, 2019 at 10:20
  • 1
    @hariprasanth those are two lines of code var value = schema[key]; and Object.keys(value).forEach(key => console.log(value[key].type)); Commented Jun 4, 2019 at 10:23

2 Answers 2

3

use Object.values then use map to return only type property.

const products_schema = {

    product_name: {
        auto: false,
        type: "string",
        min: 5,
        max: 10,
        special_characters: ['_', ' '],
        numbers: true,
        alphabet: true,
        required: true,
        correct: ""
    },
    product_image: {
        auto: false,
        type: "array:string",
        min: 0,
        max: 50,
        required: true
    }
}

const specification_schema = {
    brand: {
        auto: false,
        type: "string",
        min: 10,
        max: 50,
        special_characters: ['_', ' '],
        numbers: true,
        alphabet: true,
        required: true
    }
}

let schema = {
    products_schema:products_schema,
    specification_schema:specification_schema
}

const mergedObjects = {...products_schema, ...specification_schema};

const output = Object.values(mergedObjects).map(({type}) => type);

console.log(output);

Sign up to request clarification or add additional context in comments.

1 Comment

Won't work if products_schema and specification_schema have a common key
2

You could use nested Object.values():

const products_schema={product_name:{auto:false,type:"string",min:5,max:10,special_characters:['_',' '],numbers:true,alphabet:true,required:true,correct:""},product_image:{auto:false,type:"array:string",min:0,max:50,required:true}},
    specification_schema={brand:{auto:false,type:"string",min:10,max:50,special_characters:['_',' '],numbers:true,alphabet:true,required:true}},
    schema={ products_schema, specification_schema }

Object.values(schema).forEach(o => {
  Object.values(o).forEach(a => console.log(a.type))
})

If you want to get an array of nested type you could use flatMap

const products_schema={product_name:{auto:false,type:"string",min:5,max:10,special_characters:['_',' '],numbers:true,alphabet:true,required:true,correct:""},product_image:{auto:false,type:"array:string",min:0,max:50,required:true}},
    specification_schema={brand:{auto:false,type:"string",min:10,max:50,special_characters:['_',' '],numbers:true,alphabet:true,required:true}},
    schema={ products_schema, specification_schema }

const types = Object.values(schema).flatMap(o => 
  Object.values(o).map(a => a.type)
)

console.log(types)

If flatMap is not supported, you could simply use the first snippet and push to an array instead of logging it to the console.

const output = [];

Object.values(schema).forEach(o => 
  Object.values(o).forEach(a => output.push(a.type))
)

2 Comments

flatMap is not an function error while running the code
@hariprasanth updated the answer. If flatMap is not supported, you can use the first snippet and push each type to an array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.