Reputation: 189
Here is the object I have :
var jsonData = {
"a" : {
"0" : {
"b" : {},
"c" : {},
"d" : {
"0" : {
"e" : {},
"f" : {},
"g" : {}
}
}
}
}
}
How can I remove the "0" and turn it like this?
var jsonData = {
"a" : {
"b" : {},
"c" : {},
"d" : {
"e" : {},
"f" : {},
"g" : {}
}
}
}
Upvotes: 2
Views: 1367
Reputation: 382514
You have to build a recursive function :
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));
Upvotes: 2