loop nested objects if I don't know how much nodes/children they have?
How can I loop nested objects if I don't know how much children they have?
For instance, if I wanted to update an old object with another object (code sample), I'll need to go through every node in the objects, to check the keys/values match, one by one.
var savedData =
"a":
"x":
"foo": 1,
"foofoo": 11
,
"y":
"bar": 2,
"barbar": 22
,
"b":
//...
,
"c":
//...
;
var newData =
"a":
"x":
"foo": 7 //<== new value to be changed;
//<== notice there were "foofoo" key here, we need to keep this key and does NOT remove it;
,
"y":
"bar": 8 //<== new value to be changed;
//<== notice there were "barbar" key here, we need to keep this key and does NOT remove it;
,
"z": //<== this is a brand new child object to be added to the savedData;
"baz": 9
;
updateSavedData(newData);
function updateSavedData(newData)
if (savedData)
Object.keys(savedData).forEach(function(savedKeyLevel1)
Object.keys(newData).forEach(function(newKeyLevel1)
if (savedKeyLevel1 === newKeyLevel1)
console.log('the key [' + savedKeyLevel1 + '] exist among the saved and the new data!');
if (jQuery.type(savedData[savedKeyLevel1]) === "object")
console.log('the key [' + savedKeyLevel1 + '] is an object!');
//start looping again, but this time in a deeper level of this child object...
Object.keys(savedData[savedKeyLevel1]).forEach(function(savedKeyLevel2)
Object.keys(newData[newKeyLevel1]).forEach(function(newKeyLevel2)
if (savedKeyLevel2 === newKeyLevel2)
console.log('the key [' + savedKeyLevel1 + ' -> ' + savedKeyLevel2 + '] exist among the saved and the new data!');
//...
//<== my question is about what to do here?
//if I don't know how much deeper the savedData is.
else
//this is a brand new child object, add it to the tree;
savedData[savedKeyLevel1][newKeyLevel2] = newData[newKeyLevel1][newKeyLevel2];
);
);
else
//this is a brand new child object, add it to the tree;
savedData[newKeyLevel1] = newData[newKeyLevel1];
);
);
else
savedData = newData;
console.log('The savedData after update is:n', JSON.stringify(savedData));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
My question is:
What if I don't know how much deeper the objects are?
What am I looking for here to read/try?
javascript jquery
add a comment |
How can I loop nested objects if I don't know how much children they have?
For instance, if I wanted to update an old object with another object (code sample), I'll need to go through every node in the objects, to check the keys/values match, one by one.
var savedData =
"a":
"x":
"foo": 1,
"foofoo": 11
,
"y":
"bar": 2,
"barbar": 22
,
"b":
//...
,
"c":
//...
;
var newData =
"a":
"x":
"foo": 7 //<== new value to be changed;
//<== notice there were "foofoo" key here, we need to keep this key and does NOT remove it;
,
"y":
"bar": 8 //<== new value to be changed;
//<== notice there were "barbar" key here, we need to keep this key and does NOT remove it;
,
"z": //<== this is a brand new child object to be added to the savedData;
"baz": 9
;
updateSavedData(newData);
function updateSavedData(newData)
if (savedData)
Object.keys(savedData).forEach(function(savedKeyLevel1)
Object.keys(newData).forEach(function(newKeyLevel1)
if (savedKeyLevel1 === newKeyLevel1)
console.log('the key [' + savedKeyLevel1 + '] exist among the saved and the new data!');
if (jQuery.type(savedData[savedKeyLevel1]) === "object")
console.log('the key [' + savedKeyLevel1 + '] is an object!');
//start looping again, but this time in a deeper level of this child object...
Object.keys(savedData[savedKeyLevel1]).forEach(function(savedKeyLevel2)
Object.keys(newData[newKeyLevel1]).forEach(function(newKeyLevel2)
if (savedKeyLevel2 === newKeyLevel2)
console.log('the key [' + savedKeyLevel1 + ' -> ' + savedKeyLevel2 + '] exist among the saved and the new data!');
//...
//<== my question is about what to do here?
//if I don't know how much deeper the savedData is.
else
//this is a brand new child object, add it to the tree;
savedData[savedKeyLevel1][newKeyLevel2] = newData[newKeyLevel1][newKeyLevel2];
);
);
else
//this is a brand new child object, add it to the tree;
savedData[newKeyLevel1] = newData[newKeyLevel1];
);
);
else
savedData = newData;
console.log('The savedData after update is:n', JSON.stringify(savedData));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
My question is:
What if I don't know how much deeper the objects are?
What am I looking for here to read/try?
javascript jquery
are you aware of JSON patching? a standard way to do something similar: jsonpatch.com
– Benjaco
Nov 11 '18 at 23:26
Possible duplicate of How to deep merge instead of shallow merge?
– shkaper
Nov 11 '18 at 23:30
add a comment |
How can I loop nested objects if I don't know how much children they have?
For instance, if I wanted to update an old object with another object (code sample), I'll need to go through every node in the objects, to check the keys/values match, one by one.
var savedData =
"a":
"x":
"foo": 1,
"foofoo": 11
,
"y":
"bar": 2,
"barbar": 22
,
"b":
//...
,
"c":
//...
;
var newData =
"a":
"x":
"foo": 7 //<== new value to be changed;
//<== notice there were "foofoo" key here, we need to keep this key and does NOT remove it;
,
"y":
"bar": 8 //<== new value to be changed;
//<== notice there were "barbar" key here, we need to keep this key and does NOT remove it;
,
"z": //<== this is a brand new child object to be added to the savedData;
"baz": 9
;
updateSavedData(newData);
function updateSavedData(newData)
if (savedData)
Object.keys(savedData).forEach(function(savedKeyLevel1)
Object.keys(newData).forEach(function(newKeyLevel1)
if (savedKeyLevel1 === newKeyLevel1)
console.log('the key [' + savedKeyLevel1 + '] exist among the saved and the new data!');
if (jQuery.type(savedData[savedKeyLevel1]) === "object")
console.log('the key [' + savedKeyLevel1 + '] is an object!');
//start looping again, but this time in a deeper level of this child object...
Object.keys(savedData[savedKeyLevel1]).forEach(function(savedKeyLevel2)
Object.keys(newData[newKeyLevel1]).forEach(function(newKeyLevel2)
if (savedKeyLevel2 === newKeyLevel2)
console.log('the key [' + savedKeyLevel1 + ' -> ' + savedKeyLevel2 + '] exist among the saved and the new data!');
//...
//<== my question is about what to do here?
//if I don't know how much deeper the savedData is.
else
//this is a brand new child object, add it to the tree;
savedData[savedKeyLevel1][newKeyLevel2] = newData[newKeyLevel1][newKeyLevel2];
);
);
else
//this is a brand new child object, add it to the tree;
savedData[newKeyLevel1] = newData[newKeyLevel1];
);
);
else
savedData = newData;
console.log('The savedData after update is:n', JSON.stringify(savedData));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
My question is:
What if I don't know how much deeper the objects are?
What am I looking for here to read/try?
javascript jquery
How can I loop nested objects if I don't know how much children they have?
For instance, if I wanted to update an old object with another object (code sample), I'll need to go through every node in the objects, to check the keys/values match, one by one.
var savedData =
"a":
"x":
"foo": 1,
"foofoo": 11
,
"y":
"bar": 2,
"barbar": 22
,
"b":
//...
,
"c":
//...
;
var newData =
"a":
"x":
"foo": 7 //<== new value to be changed;
//<== notice there were "foofoo" key here, we need to keep this key and does NOT remove it;
,
"y":
"bar": 8 //<== new value to be changed;
//<== notice there were "barbar" key here, we need to keep this key and does NOT remove it;
,
"z": //<== this is a brand new child object to be added to the savedData;
"baz": 9
;
updateSavedData(newData);
function updateSavedData(newData)
if (savedData)
Object.keys(savedData).forEach(function(savedKeyLevel1)
Object.keys(newData).forEach(function(newKeyLevel1)
if (savedKeyLevel1 === newKeyLevel1)
console.log('the key [' + savedKeyLevel1 + '] exist among the saved and the new data!');
if (jQuery.type(savedData[savedKeyLevel1]) === "object")
console.log('the key [' + savedKeyLevel1 + '] is an object!');
//start looping again, but this time in a deeper level of this child object...
Object.keys(savedData[savedKeyLevel1]).forEach(function(savedKeyLevel2)
Object.keys(newData[newKeyLevel1]).forEach(function(newKeyLevel2)
if (savedKeyLevel2 === newKeyLevel2)
console.log('the key [' + savedKeyLevel1 + ' -> ' + savedKeyLevel2 + '] exist among the saved and the new data!');
//...
//<== my question is about what to do here?
//if I don't know how much deeper the savedData is.
else
//this is a brand new child object, add it to the tree;
savedData[savedKeyLevel1][newKeyLevel2] = newData[newKeyLevel1][newKeyLevel2];
);
);
else
//this is a brand new child object, add it to the tree;
savedData[newKeyLevel1] = newData[newKeyLevel1];
);
);
else
savedData = newData;
console.log('The savedData after update is:n', JSON.stringify(savedData));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
My question is:
What if I don't know how much deeper the objects are?
What am I looking for here to read/try?
var savedData =
"a":
"x":
"foo": 1,
"foofoo": 11
,
"y":
"bar": 2,
"barbar": 22
,
"b":
//...
,
"c":
//...
;
var newData =
"a":
"x":
"foo": 7 //<== new value to be changed;
//<== notice there were "foofoo" key here, we need to keep this key and does NOT remove it;
,
"y":
"bar": 8 //<== new value to be changed;
//<== notice there were "barbar" key here, we need to keep this key and does NOT remove it;
,
"z": //<== this is a brand new child object to be added to the savedData;
"baz": 9
;
updateSavedData(newData);
function updateSavedData(newData)
if (savedData)
Object.keys(savedData).forEach(function(savedKeyLevel1)
Object.keys(newData).forEach(function(newKeyLevel1)
if (savedKeyLevel1 === newKeyLevel1)
console.log('the key [' + savedKeyLevel1 + '] exist among the saved and the new data!');
if (jQuery.type(savedData[savedKeyLevel1]) === "object")
console.log('the key [' + savedKeyLevel1 + '] is an object!');
//start looping again, but this time in a deeper level of this child object...
Object.keys(savedData[savedKeyLevel1]).forEach(function(savedKeyLevel2)
Object.keys(newData[newKeyLevel1]).forEach(function(newKeyLevel2)
if (savedKeyLevel2 === newKeyLevel2)
console.log('the key [' + savedKeyLevel1 + ' -> ' + savedKeyLevel2 + '] exist among the saved and the new data!');
//...
//<== my question is about what to do here?
//if I don't know how much deeper the savedData is.
else
//this is a brand new child object, add it to the tree;
savedData[savedKeyLevel1][newKeyLevel2] = newData[newKeyLevel1][newKeyLevel2];
);
);
else
//this is a brand new child object, add it to the tree;
savedData[newKeyLevel1] = newData[newKeyLevel1];
);
);
else
savedData = newData;
console.log('The savedData after update is:n', JSON.stringify(savedData));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
var savedData =
"a":
"x":
"foo": 1,
"foofoo": 11
,
"y":
"bar": 2,
"barbar": 22
,
"b":
//...
,
"c":
//...
;
var newData =
"a":
"x":
"foo": 7 //<== new value to be changed;
//<== notice there were "foofoo" key here, we need to keep this key and does NOT remove it;
,
"y":
"bar": 8 //<== new value to be changed;
//<== notice there were "barbar" key here, we need to keep this key and does NOT remove it;
,
"z": //<== this is a brand new child object to be added to the savedData;
"baz": 9
;
updateSavedData(newData);
function updateSavedData(newData)
if (savedData)
Object.keys(savedData).forEach(function(savedKeyLevel1)
Object.keys(newData).forEach(function(newKeyLevel1)
if (savedKeyLevel1 === newKeyLevel1)
console.log('the key [' + savedKeyLevel1 + '] exist among the saved and the new data!');
if (jQuery.type(savedData[savedKeyLevel1]) === "object")
console.log('the key [' + savedKeyLevel1 + '] is an object!');
//start looping again, but this time in a deeper level of this child object...
Object.keys(savedData[savedKeyLevel1]).forEach(function(savedKeyLevel2)
Object.keys(newData[newKeyLevel1]).forEach(function(newKeyLevel2)
if (savedKeyLevel2 === newKeyLevel2)
console.log('the key [' + savedKeyLevel1 + ' -> ' + savedKeyLevel2 + '] exist among the saved and the new data!');
//...
//<== my question is about what to do here?
//if I don't know how much deeper the savedData is.
else
//this is a brand new child object, add it to the tree;
savedData[savedKeyLevel1][newKeyLevel2] = newData[newKeyLevel1][newKeyLevel2];
);
);
else
//this is a brand new child object, add it to the tree;
savedData[newKeyLevel1] = newData[newKeyLevel1];
);
);
else
savedData = newData;
console.log('The savedData after update is:n', JSON.stringify(savedData));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
javascript jquery
javascript jquery
edited Nov 11 '18 at 23:29
user7607751
asked Nov 11 '18 at 23:19
user7607751user7607751
3810
3810
are you aware of JSON patching? a standard way to do something similar: jsonpatch.com
– Benjaco
Nov 11 '18 at 23:26
Possible duplicate of How to deep merge instead of shallow merge?
– shkaper
Nov 11 '18 at 23:30
add a comment |
are you aware of JSON patching? a standard way to do something similar: jsonpatch.com
– Benjaco
Nov 11 '18 at 23:26
Possible duplicate of How to deep merge instead of shallow merge?
– shkaper
Nov 11 '18 at 23:30
are you aware of JSON patching? a standard way to do something similar: jsonpatch.com
– Benjaco
Nov 11 '18 at 23:26
are you aware of JSON patching? a standard way to do something similar: jsonpatch.com
– Benjaco
Nov 11 '18 at 23:26
Possible duplicate of How to deep merge instead of shallow merge?
– shkaper
Nov 11 '18 at 23:30
Possible duplicate of How to deep merge instead of shallow merge?
– shkaper
Nov 11 '18 at 23:30
add a comment |
2 Answers
2
active
oldest
votes
You may be making this a little more complicated than it needs to be. So long as it's a simple object with nested objects and properties, you can just test for an object and recurse:
var savedData = "a": "x": "foo": 1,"foofoo": 11,"y": "bar": 2,"barbar": 22,"b": ,"c": ;
var newData = "a": "x": "foo": 7,"y": "bar": 8 ,"z": "baz": 9;
function merge(data, newdata)
for (let key in newdata)
if (typeof data[key] == 'object') merge(data[key], newdata[key])
else data[key] = newdata[key]
merge(savedData, newData)
console.log(savedData)
wow! ^_^ yeah, I was really over complicating it! :)
– user7607751
Nov 11 '18 at 23:54
This is the first time I came across or clearly notice calling a function from inside itself! Thank you Mark :)
– user7607751
Nov 11 '18 at 23:56
1
@user7607751 when a function calls itself, it's a recursive function. There's a short overview if you scroll down the MDN guide on functions.
– Mark Meyer
Nov 12 '18 at 0:04
1
Yes, I sure need to read about these concepts, thank you again Mark!
– user7607751
Nov 12 '18 at 0:07
add a comment |
Mark's answer is probably the most efficient.
But if you are truly looking to loop through nested objects, you might use recursivity :
function loopThroughNewData(newData)
if(newData && Object.keys(newData).length > 0)
Object.keys(newData).forEach(function(newKeyLevel)
//do stuff
return loopThroughNewData(newData[newKeyLevel]);
else
return something;
Yeah, I got to say here the same comment I said to Mark: "This is the first time I came across or clearly notice calling a function from inside itself!" haha. Thank you Al :) I appreciate it!
– user7607751
Nov 12 '18 at 0:01
yes,recursivity
is the key here.
– user7607751
Nov 12 '18 at 0:08
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',
autoActivateHeartbeat: false,
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%2f53254231%2floop-nested-objects-if-i-dont-know-how-much-nodes-children-they-have%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
You may be making this a little more complicated than it needs to be. So long as it's a simple object with nested objects and properties, you can just test for an object and recurse:
var savedData = "a": "x": "foo": 1,"foofoo": 11,"y": "bar": 2,"barbar": 22,"b": ,"c": ;
var newData = "a": "x": "foo": 7,"y": "bar": 8 ,"z": "baz": 9;
function merge(data, newdata)
for (let key in newdata)
if (typeof data[key] == 'object') merge(data[key], newdata[key])
else data[key] = newdata[key]
merge(savedData, newData)
console.log(savedData)
wow! ^_^ yeah, I was really over complicating it! :)
– user7607751
Nov 11 '18 at 23:54
This is the first time I came across or clearly notice calling a function from inside itself! Thank you Mark :)
– user7607751
Nov 11 '18 at 23:56
1
@user7607751 when a function calls itself, it's a recursive function. There's a short overview if you scroll down the MDN guide on functions.
– Mark Meyer
Nov 12 '18 at 0:04
1
Yes, I sure need to read about these concepts, thank you again Mark!
– user7607751
Nov 12 '18 at 0:07
add a comment |
You may be making this a little more complicated than it needs to be. So long as it's a simple object with nested objects and properties, you can just test for an object and recurse:
var savedData = "a": "x": "foo": 1,"foofoo": 11,"y": "bar": 2,"barbar": 22,"b": ,"c": ;
var newData = "a": "x": "foo": 7,"y": "bar": 8 ,"z": "baz": 9;
function merge(data, newdata)
for (let key in newdata)
if (typeof data[key] == 'object') merge(data[key], newdata[key])
else data[key] = newdata[key]
merge(savedData, newData)
console.log(savedData)
wow! ^_^ yeah, I was really over complicating it! :)
– user7607751
Nov 11 '18 at 23:54
This is the first time I came across or clearly notice calling a function from inside itself! Thank you Mark :)
– user7607751
Nov 11 '18 at 23:56
1
@user7607751 when a function calls itself, it's a recursive function. There's a short overview if you scroll down the MDN guide on functions.
– Mark Meyer
Nov 12 '18 at 0:04
1
Yes, I sure need to read about these concepts, thank you again Mark!
– user7607751
Nov 12 '18 at 0:07
add a comment |
You may be making this a little more complicated than it needs to be. So long as it's a simple object with nested objects and properties, you can just test for an object and recurse:
var savedData = "a": "x": "foo": 1,"foofoo": 11,"y": "bar": 2,"barbar": 22,"b": ,"c": ;
var newData = "a": "x": "foo": 7,"y": "bar": 8 ,"z": "baz": 9;
function merge(data, newdata)
for (let key in newdata)
if (typeof data[key] == 'object') merge(data[key], newdata[key])
else data[key] = newdata[key]
merge(savedData, newData)
console.log(savedData)
You may be making this a little more complicated than it needs to be. So long as it's a simple object with nested objects and properties, you can just test for an object and recurse:
var savedData = "a": "x": "foo": 1,"foofoo": 11,"y": "bar": 2,"barbar": 22,"b": ,"c": ;
var newData = "a": "x": "foo": 7,"y": "bar": 8 ,"z": "baz": 9;
function merge(data, newdata)
for (let key in newdata)
if (typeof data[key] == 'object') merge(data[key], newdata[key])
else data[key] = newdata[key]
merge(savedData, newData)
console.log(savedData)
var savedData = "a": "x": "foo": 1,"foofoo": 11,"y": "bar": 2,"barbar": 22,"b": ,"c": ;
var newData = "a": "x": "foo": 7,"y": "bar": 8 ,"z": "baz": 9;
function merge(data, newdata)
for (let key in newdata)
if (typeof data[key] == 'object') merge(data[key], newdata[key])
else data[key] = newdata[key]
merge(savedData, newData)
console.log(savedData)
var savedData = "a": "x": "foo": 1,"foofoo": 11,"y": "bar": 2,"barbar": 22,"b": ,"c": ;
var newData = "a": "x": "foo": 7,"y": "bar": 8 ,"z": "baz": 9;
function merge(data, newdata)
for (let key in newdata)
if (typeof data[key] == 'object') merge(data[key], newdata[key])
else data[key] = newdata[key]
merge(savedData, newData)
console.log(savedData)
answered Nov 11 '18 at 23:44
Mark MeyerMark Meyer
38.6k33159
38.6k33159
wow! ^_^ yeah, I was really over complicating it! :)
– user7607751
Nov 11 '18 at 23:54
This is the first time I came across or clearly notice calling a function from inside itself! Thank you Mark :)
– user7607751
Nov 11 '18 at 23:56
1
@user7607751 when a function calls itself, it's a recursive function. There's a short overview if you scroll down the MDN guide on functions.
– Mark Meyer
Nov 12 '18 at 0:04
1
Yes, I sure need to read about these concepts, thank you again Mark!
– user7607751
Nov 12 '18 at 0:07
add a comment |
wow! ^_^ yeah, I was really over complicating it! :)
– user7607751
Nov 11 '18 at 23:54
This is the first time I came across or clearly notice calling a function from inside itself! Thank you Mark :)
– user7607751
Nov 11 '18 at 23:56
1
@user7607751 when a function calls itself, it's a recursive function. There's a short overview if you scroll down the MDN guide on functions.
– Mark Meyer
Nov 12 '18 at 0:04
1
Yes, I sure need to read about these concepts, thank you again Mark!
– user7607751
Nov 12 '18 at 0:07
wow! ^_^ yeah, I was really over complicating it! :)
– user7607751
Nov 11 '18 at 23:54
wow! ^_^ yeah, I was really over complicating it! :)
– user7607751
Nov 11 '18 at 23:54
This is the first time I came across or clearly notice calling a function from inside itself! Thank you Mark :)
– user7607751
Nov 11 '18 at 23:56
This is the first time I came across or clearly notice calling a function from inside itself! Thank you Mark :)
– user7607751
Nov 11 '18 at 23:56
1
1
@user7607751 when a function calls itself, it's a recursive function. There's a short overview if you scroll down the MDN guide on functions.
– Mark Meyer
Nov 12 '18 at 0:04
@user7607751 when a function calls itself, it's a recursive function. There's a short overview if you scroll down the MDN guide on functions.
– Mark Meyer
Nov 12 '18 at 0:04
1
1
Yes, I sure need to read about these concepts, thank you again Mark!
– user7607751
Nov 12 '18 at 0:07
Yes, I sure need to read about these concepts, thank you again Mark!
– user7607751
Nov 12 '18 at 0:07
add a comment |
Mark's answer is probably the most efficient.
But if you are truly looking to loop through nested objects, you might use recursivity :
function loopThroughNewData(newData)
if(newData && Object.keys(newData).length > 0)
Object.keys(newData).forEach(function(newKeyLevel)
//do stuff
return loopThroughNewData(newData[newKeyLevel]);
else
return something;
Yeah, I got to say here the same comment I said to Mark: "This is the first time I came across or clearly notice calling a function from inside itself!" haha. Thank you Al :) I appreciate it!
– user7607751
Nov 12 '18 at 0:01
yes,recursivity
is the key here.
– user7607751
Nov 12 '18 at 0:08
add a comment |
Mark's answer is probably the most efficient.
But if you are truly looking to loop through nested objects, you might use recursivity :
function loopThroughNewData(newData)
if(newData && Object.keys(newData).length > 0)
Object.keys(newData).forEach(function(newKeyLevel)
//do stuff
return loopThroughNewData(newData[newKeyLevel]);
else
return something;
Yeah, I got to say here the same comment I said to Mark: "This is the first time I came across or clearly notice calling a function from inside itself!" haha. Thank you Al :) I appreciate it!
– user7607751
Nov 12 '18 at 0:01
yes,recursivity
is the key here.
– user7607751
Nov 12 '18 at 0:08
add a comment |
Mark's answer is probably the most efficient.
But if you are truly looking to loop through nested objects, you might use recursivity :
function loopThroughNewData(newData)
if(newData && Object.keys(newData).length > 0)
Object.keys(newData).forEach(function(newKeyLevel)
//do stuff
return loopThroughNewData(newData[newKeyLevel]);
else
return something;
Mark's answer is probably the most efficient.
But if you are truly looking to loop through nested objects, you might use recursivity :
function loopThroughNewData(newData)
if(newData && Object.keys(newData).length > 0)
Object.keys(newData).forEach(function(newKeyLevel)
//do stuff
return loopThroughNewData(newData[newKeyLevel]);
else
return something;
answered Nov 11 '18 at 23:57
AllirionXAllirionX
1765
1765
Yeah, I got to say here the same comment I said to Mark: "This is the first time I came across or clearly notice calling a function from inside itself!" haha. Thank you Al :) I appreciate it!
– user7607751
Nov 12 '18 at 0:01
yes,recursivity
is the key here.
– user7607751
Nov 12 '18 at 0:08
add a comment |
Yeah, I got to say here the same comment I said to Mark: "This is the first time I came across or clearly notice calling a function from inside itself!" haha. Thank you Al :) I appreciate it!
– user7607751
Nov 12 '18 at 0:01
yes,recursivity
is the key here.
– user7607751
Nov 12 '18 at 0:08
Yeah, I got to say here the same comment I said to Mark: "This is the first time I came across or clearly notice calling a function from inside itself!" haha. Thank you Al :) I appreciate it!
– user7607751
Nov 12 '18 at 0:01
Yeah, I got to say here the same comment I said to Mark: "This is the first time I came across or clearly notice calling a function from inside itself!" haha. Thank you Al :) I appreciate it!
– user7607751
Nov 12 '18 at 0:01
yes,
recursivity
is the key here.– user7607751
Nov 12 '18 at 0:08
yes,
recursivity
is the key here.– user7607751
Nov 12 '18 at 0:08
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.
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%2f53254231%2floop-nested-objects-if-i-dont-know-how-much-nodes-children-they-have%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
are you aware of JSON patching? a standard way to do something similar: jsonpatch.com
– Benjaco
Nov 11 '18 at 23:26
Possible duplicate of How to deep merge instead of shallow merge?
– shkaper
Nov 11 '18 at 23:30