I have the following object, and want to set all the values in it to null, while maintaining the objects structure
var objs = {
a: {
prop1: {id: null, val: 10},
prop2: true,
prop3: null,
},
b: {
prop1: {id: null, val: 20},
prop2: true,
prop3: null,
},
c: 10
}
The results needs to be:
var objs = {
a: {
prop1: {id: null, val: null},
prop2: null,
prop3: null,
},
b: {
prop1: {id: null, val: null},
prop2: null,
prop3: null,
},
c: null
}
This solution works for me, and I believe it should work for any depth. But if there is more elegant or efficient solution, please let me know.
var objs = {
a: {
prop1: {id: null, val: 10},
prop2: true,
prop3: null,
},
b: {
prop1: {id: null, val: 20},
prop2: true,
prop3: null,
},
c: 10
}
function nullTest(val){
for (key in val) {
if(typeof(val[key]) !== 'object') {
val[key] = null;
} else {
val[key] = nullTest(val[key])
}
}
return val
}
console.log(nullTest(objs));
Thanks