这是我拥有的对象:
var jsonData = {
"a" : {
"0" : {
"b" : {},
"c" : {},
"d" : {
"0" : {
"e" : {},
"f" : {},
"g" : {}
}
}
}
}
}
如何删除“0”并将其翻转?
var jsonData = {
"a" : {
"b" : {},
"c" : {},
"d" : {
"e" : {},
"f" : {},
"g" : {}
}
}
}
答案 0 :(得分:2)
你必须建立一个递归函数:
var jsonData = {
"a" : {
"0" : {
"b" : {},
"c" : {},
"d" : {
"0" : {
"e" : {},
"f" : {},
"g" : {},
"h" : 3 // added to be less trivial
}
}
}
}
}
function cloneWithout0(v){
if (typeof v !== "object") return v;
var c = {};
for (var k in v) {
if (k !== '0') c[k] = cloneWithout0(v[k]);
}
if (v['0']) {
for (var k in v['0']) {
if (k !== '0') c[k] = cloneWithout0(v['0'][k]);
}
}
return c;
}
console.log(cloneWithout0(jsonData));