Reputation: 61
I have an object obj1
with another object inside obj2
.
obj2
is structured in the same way as obj1
so there will be another object inside it.
Let's say that I have 20 of these and I'm trying to get inside each one of them to get some data.
Is there a way to create a loop that goes inside an obj as soon as it sees it?
I tried doing this but with no luck.
var location;
for (var [key, value] of Object.entries(object)) {
var type = typeof value;
var array = Array.isArray(value);
if (typeof value === "object" && array === false && value) {
location = key;
for (var [a, b] of Object.entries(object[location])) {
/*this is where I'm stuck, the location variable doesn't update with
the 'path' of every object*/
}
}
}
Upvotes: 0
Views: 33
Reputation: 3411
Create a function that loop on one level. Check values for objects. If you find one, recall the same function.
function createLoop(obj) {
for (const key in obj) {
if (typeof obj[key] == "object") {
createLoop(obj[key])
} else {
// do your stuff
}
}
}
Upvotes: 1