我知道这个问题已经被问了很多次,但是我没有找到一个能够帮助我解决的解决方案,因为我的json对象结构可能有问题。
这是我的json:
check_styles = {
'html':{
'background-color':'rgb(0, 0, 0)',
'color':'rgb(0, 0, 0)'
},
'#my-div':{
'background-color':'rgb(0, 0, 0)'
}
};
我想循环遍历并获取值
'html','background-color','rgb(0, 0, 0)'
,然后'html','colour','rgb(0, 0, 0)'
等以发送到一个函数。以下是迄今为止的循环,但我无法获取对象中对象的值。 我认为另一个循环不是答案。
function style_check(styleList)
{
for (var key in styleList) {
if (styleList.hasOwnProperty(key) ){
console.log( "key:"+key+", val:"+styleList[key] );
}
}
}
****我的解决方案
在经过三种完全有效的解决方案后,我选择了嵌套循环,因为在我有限的javascript知识下,这对我来说是有意义的。
function style_check(styleList)
{
for (var selector in styleList) {
if (styleList.hasOwnProperty(selector) ){
for (var property in styleList[selector]) {
if (styleList[selector].hasOwnProperty(property) ){
console.log( "selector:"+selector+", property:"+property+", value:"+styleList[selector][property] );
}
}
}
}
}