How can I loop though each of this objects nested properties and set them all to null? I only need to go 2 levels deep so any props that are objects need to be null also.
var objs = {
a: {
prop1: {id: null, ctx: CanvasRenderingContext2D},
prop2: true,
prop3: null,
prop4: null,
prop5: true,
prop6: null,
prop7: null,
prop8: true,
prop9: null,
prop10: null,
prop11: true,
},
b: {
prop1: {id: null, ctx: CanvasRenderingContext2D},
prop2: true,
prop3: null,
prop4: null,
prop5: true,
prop6: null,
prop7: null,
prop8: true,
},
c: {
prop1: {id: null, ctx: CanvasRenderingContext2D},
prop2: true,
prop3: null,
prop4: null,
prop5: true,
}
}
I have tried this but it's going into the prop1 object which I don't want it to.
function nullify (obj) {
for(key in obj) {
if (typeof obj[key] == "object") {
obj[key] = nullify(obj[key]);
}
else if(obj[key] != null) {
obj[key] = null;
}
}
return obj;
}
nullify (objs)
I haver also tried this but this goes through each letter of the outer key not the inner properties
for (obj in objs) {
if (objs.hasOwnProperty(obj)) {
for (key in obj) {
if (obj.hasOwnProperty(key)) {
obj[key] = null;
}
}
}
}