我有一个3级深度对象,具有相同的结构化子对象:
var data = {
0:{"level":1,
"data": {
0:{"level":2,
"data": {
0:{"level":3,
"data": null},
1:{"level":3,
"data": null}
}
},
1:{"level":2,
"data":{
0:{"level":3,
"data": null},
1:{"level":3,
"data": null}
}
}
}
}}
和递归函数:
$.each(data, function(k,v){
rfunc(k,v)
});
function rfunc(key, val) {
console.log(val.level)
if ( val.data ) {
$.each(val.data, function(key, val) {
rfunc(key, val)
});
}
}
正如预期的那样控制台:
1
2
3
3
2
3
3
当程序将级别提升到“2”
感谢您的关注!
答案 0 :(得分:0)
使用一个解决方案(递归):http://jsfiddle.net/px3KT/
var data = {
0:{"level":1,
"data": {
0:{"level":2,
"data": {
0:{"level":3,
"data": null},
1:{"level":3,
"data": null}
}
},
1:{"level":2,
"data":{
0:{"level":3,
"data": null},
1:{"level":3,
"data": null}
}
}
}
}}
function recurseData(obj,level){
console.log(level);
if(obj){
$.each(obj, function(key, val) {
var level = val.level;
var data_obj = val.data;
recurseData(data_obj,level);
});
}
}
recurseData(data);