javascript - How to iterate a nested object using map function? - Stack Overflow

I had used for loop to iterate nested objects, I amtrying to replace forEach with the map function, w

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));
}

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

Share Improve this question edited Jun 4, 2019 at 10:29 GrafiCode 3,3743 gold badges29 silver badges32 bronze badges asked Jun 4, 2019 at 10:15 hari prasanthhari prasanth 7464 gold badges21 silver badges39 bronze badges 8
  • What's wrong with for loop? – hindmost 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)); }) – Harish Commented Jun 4, 2019 at 10:19
  • for loop works fine. But my requirement is to replace for with map – hari prasanth Commented Jun 4, 2019 at 10:20
  • What is the expected value of schema? – Nikhil Aggarwal 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)); – GrafiCode Commented Jun 4, 2019 at 10:23
 |  Show 3 more ments

2 Answers 2

Reset to default 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);

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))
)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744218268a4563656.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信