我有一个看起来像这样的对象
const userAnswers = {
1: {id: 3, value: "Assistant manager"},
2: {id: 1, value: "I am the primary decision maker"},
3: {
1: {id: 1, name: "Water quality management", value: "On a long list of priorities", valueId: 2},
2: {id: 2, name: "Greenhouse gas reduction", value: "On a long list of priorities", valueId: 2},
3: {id: 3, name: "Finanicial management", value: "On a long list of priorities", valueId: 2},
4: {id: 4, name: "Feed management", value: "On a long list of priorities", valueId: 2}
}
}
我知道我可以以此遍历整个对象
Object.values(answers).forEach(value => { console.log(value)} )
但是是否只想遍历3:
内部的嵌套对象?
答案 0 :(得分:0)
您可以尝试
Object.values(Object.values(object1)[2]).forEach(item => console.log(item));
答案 1 :(得分:0)
就像我在评论中说的那样,您可以遍历userAnswers['3']
的值,并通过方括号(object["keyName"]
)对其进行访问
const userAnswers = {
1: {id: 3, value: "Assistant manager"},
2: {id: 1, value: "I am the primary decision maker"},
3: {
1: {id: 1, name: "Water quality management", value: "On a long list of priorities", valueId: 2},
2: {id: 2, name: "Greenhouse gas reduction", value: "On a long list of priorities", valueId: 2},
3: {id: 3, name: "Finanicial management", value: "On a long list of priorities", valueId: 2},
4: {id: 4, name: "Feed management", value: "On a long list of priorities", valueId: 2}
}
}
var objectWithKey3 = userAnswers['3']
for (var obj of Object.values(objectWithKey3)){
console.log(obj)
}