JavaScript set all nested object properties to null











up vote
1
down vote

favorite












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









share|improve this question




















  • 2




    The data structure screams to be an array instead of nested objects. Then you can just loop with very basic syntax.
    – Shilly
    Nov 20 at 16:09















up vote
1
down vote

favorite












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









share|improve this question




















  • 2




    The data structure screams to be an array instead of nested objects. Then you can just loop with very basic syntax.
    – Shilly
    Nov 20 at 16:09













up vote
1
down vote

favorite









up vote
1
down vote

favorite











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









share|improve this question















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






javascript for-loop javascript-objects






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 17:34









wanttobeprofessional

89931223




89931223










asked Nov 20 at 16:03









AdRock

1,77094680




1,77094680








  • 2




    The data structure screams to be an array instead of nested objects. Then you can just loop with very basic syntax.
    – Shilly
    Nov 20 at 16:09














  • 2




    The data structure screams to be an array instead of nested objects. Then you can just loop with very basic syntax.
    – Shilly
    Nov 20 at 16:09








2




2




The data structure screams to be an array instead of nested objects. Then you can just loop with very basic syntax.
– Shilly
Nov 20 at 16:09




The data structure screams to be an array instead of nested objects. Then you can just loop with very basic syntax.
– Shilly
Nov 20 at 16:09












2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










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)








share|improve this answer

















  • 1




    That was it. 3 lines of code did what I wanted.
    – AdRock
    Nov 20 at 16:28


















up vote
0
down vote













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)






share|improve this answer



















  • 1




    You should show how can use of 'depth' parameter.
    – Hanif
    Nov 20 at 16:12











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53396958%2fjavascript-set-all-nested-object-properties-to-null%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










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)








share|improve this answer

















  • 1




    That was it. 3 lines of code did what I wanted.
    – AdRock
    Nov 20 at 16:28















up vote
3
down vote



accepted










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)








share|improve this answer

















  • 1




    That was it. 3 lines of code did what I wanted.
    – AdRock
    Nov 20 at 16:28













up vote
3
down vote



accepted







up vote
3
down vote



accepted






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)








share|improve this answer












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)








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)





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)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 at 16:13









Mark Meyer

33.6k32854




33.6k32854








  • 1




    That was it. 3 lines of code did what I wanted.
    – AdRock
    Nov 20 at 16:28














  • 1




    That was it. 3 lines of code did what I wanted.
    – AdRock
    Nov 20 at 16:28








1




1




That was it. 3 lines of code did what I wanted.
– AdRock
Nov 20 at 16:28




That was it. 3 lines of code did what I wanted.
– AdRock
Nov 20 at 16:28












up vote
0
down vote













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)






share|improve this answer



















  • 1




    You should show how can use of 'depth' parameter.
    – Hanif
    Nov 20 at 16:12















up vote
0
down vote













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)






share|improve this answer



















  • 1




    You should show how can use of 'depth' parameter.
    – Hanif
    Nov 20 at 16:12













up vote
0
down vote










up vote
0
down vote









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)






share|improve this answer














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)







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 at 16:17

























answered Nov 20 at 16:10









dipea

213




213








  • 1




    You should show how can use of 'depth' parameter.
    – Hanif
    Nov 20 at 16:12














  • 1




    You should show how can use of 'depth' parameter.
    – Hanif
    Nov 20 at 16:12








1




1




You should show how can use of 'depth' parameter.
– Hanif
Nov 20 at 16:12




You should show how can use of 'depth' parameter.
– Hanif
Nov 20 at 16:12


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53396958%2fjavascript-set-all-nested-object-properties-to-null%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python