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 prop
s 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
add a comment |
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 prop
s 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
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
add a comment |
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 prop
s 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
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 prop
s 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
javascript for-loop javascript-objects
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
add a comment |
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
add a comment |
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)
1
That was it. 3 lines of code did what I wanted.
– AdRock
Nov 20 at 16:28
add a comment |
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)
1
You should show how can use of 'depth' parameter.
– Hanif
Nov 20 at 16:12
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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)
1
That was it. 3 lines of code did what I wanted.
– AdRock
Nov 20 at 16:28
add a comment |
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)
1
That was it. 3 lines of code did what I wanted.
– AdRock
Nov 20 at 16:28
add a comment |
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)
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)
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
add a comment |
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
add a comment |
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)
1
You should show how can use of 'depth' parameter.
– Hanif
Nov 20 at 16:12
add a comment |
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)
1
You should show how can use of 'depth' parameter.
– Hanif
Nov 20 at 16:12
add a comment |
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)
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)
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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