3

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;
            }
        }
    }
}
1
  • 2
    The data structure screams to be an array instead of nested objects. Then you can just loop with very basic syntax. Commented Nov 20, 2018 at 16:09

2 Answers 2

5

Since you only need to go two levels, you can just loop over the keys of the child objects:

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,}}

Object.values(objs).forEach(val => {
  for (key in val) val[key] = null
})
console.log(objs)

Sign up to request clarification or add additional context in comments.

Comments

0

Does this do the trick?

function nullify(obj, depth) {
    depth = depth || 0;
    if (depth > 1) return obj;
    for(key in obj) {
        if (typeof obj[key] == "object") {
          obj[key] = nullify(obj[key], depth + 1);
        }
        else if(obj[key] != null) {
            obj[key] = null;
        }
   }
   return obj;
}

Edit:

Call it without passing in a depth argument: nullify(objs)

1 Comment

It doesn't nullify the values inside at the second level namely ctx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.