如何显示 js 对象名称及其所有 属性 值?
How to show js object name and its all property value?
我有这样的对象
var savedColor = {
"plat1" : {
"background" : "rgb(0,0,0)",
"text" : "rgb(255,255,255)"
},
"plat2" : {
"background" : "rgb(0,50,50)"
"text" : "rgb(0,0,0)"
}
}
我想这样显示输出:
plate1
rgb(0,0,0)
rgb(255,255,255)
plate2
rgb(0,50,50)
rgb(0,0,0)
我试过这段代码:
for(var x in savedColor){
console.log(savedColor[x]);
for(var y in savedColor[x]){
console.log(savedColor[y]);
}
}
但是输出并不像我 want.The 输出的那样显示:
[Object object]
undefined
undefined
[Object object]
undefined
undefined
我怎样才能像上面所说的那样显示输出?
使用Object.entries
获取外部键(可以打印)和对象,然后遍历内部对象的Object.values
并打印:
var savedColor = {
"plat1" : {
"background" : "rgb(0,0,0)",
"text" : "rgb(255,255,255)"
},
"plat2" : {
"background" : "rgb(0,50,50)",
"text" : "rgb(0,0,0)"
}
}
for (const [key, obj] of Object.entries(savedColor)) {
console.log(key);
for (const value of Object.values(obj)) {
console.log(value);
}
}
我有这样的对象
var savedColor = {
"plat1" : {
"background" : "rgb(0,0,0)",
"text" : "rgb(255,255,255)"
},
"plat2" : {
"background" : "rgb(0,50,50)"
"text" : "rgb(0,0,0)"
}
}
我想这样显示输出:
plate1
rgb(0,0,0)
rgb(255,255,255)
plate2
rgb(0,50,50)
rgb(0,0,0)
我试过这段代码:
for(var x in savedColor){
console.log(savedColor[x]);
for(var y in savedColor[x]){
console.log(savedColor[y]);
}
}
但是输出并不像我 want.The 输出的那样显示:
[Object object]
undefined
undefined
[Object object]
undefined
undefined
我怎样才能像上面所说的那样显示输出?
使用Object.entries
获取外部键(可以打印)和对象,然后遍历内部对象的Object.values
并打印:
var savedColor = {
"plat1" : {
"background" : "rgb(0,0,0)",
"text" : "rgb(255,255,255)"
},
"plat2" : {
"background" : "rgb(0,50,50)",
"text" : "rgb(0,0,0)"
}
}
for (const [key, obj] of Object.entries(savedColor)) {
console.log(key);
for (const value of Object.values(obj)) {
console.log(value);
}
}