【发布时间】:2018-02-07 12:59:48
【问题描述】:
嗨,我有一个看起来像这样的嵌套对象
var dogTypes = {
GermanShepard {color: "black and white"},
Beagle {color: "brown and white"},
cheuwahwah {color: "green and white"},
poodle: {color: "purple and white"},
}
我试图遍历嵌套对象中的所有属性,我知道如何使用常规对象而不是嵌套对象来执行此操作,所以如果有人可以帮助我,那就太好了。
for (var key in dogTypes) {
console.log(key + " : " + dogTypes[key])
}
这是我打印出来的代码
GreatDane : [object Object]
GermanSheppard : [object Object]
Beagle : [object Object]
BullDog : [object Object]
我会在哪里将颜色属性合并到 for in 循环中,请帮忙!谢谢
【问题讨论】:
-
console.log(key + " : " + dogTypes[key].color)? -
如果它只有两层深,双 for 循环可以工作,如果不是递归或 while 循环是个好主意
标签: javascript loops object properties nested