如何使用地图函数迭代嵌套对象?

时间:2019-06-04 10:15:22

标签: javascript

我曾经使用for循环来迭代嵌套对象,但是我试图用forEach函数替换map,但没有成功。有人可以帮我吗?

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

“预期输出:”
字符串
数组:字符串
字符串

2 个答案:

答案 0 :(得分:3)

使用Object.values,然后使用map仅返回type属性。

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

答案 1 :(得分:2)

您可以使用嵌套的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))
})

如果要获取嵌套的type数组,可以使用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)

如果不支持flatMap,则只需将第一个代码段和push用于数组,而不是将其记录到控制台。

const output = [];

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