Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to iterate over inner objects / property in an object

I have an object which has inner objects and properties defined like this:

var obj = {obj1 : { "prop1" : "nothing", "prop2" : "prop"},
        obj2 : {"prop1" : "nothing", "prop2" : "prop"},
        pr1 : "message",
        pr2 : "mess"
};

Normally to iterate every property of an object , the for .. in loop can do the trick

for (property in obj){
    if (obj.hasOwnProperty(property)){
        console.log(property + "   " + obj[property]);
    }
}

the console displayed :

obj1 [object Object]
obj12 [object Object]
pr1 message
pr2 mess

However how to iterate the inner objects (obj1, obj2) and their own properties (prop1,prop2) ?

like image 630
Raymond Chenon Avatar asked Mar 25 '11 13:03

Raymond Chenon


People also ask

How do I iterate an object within an object?

There are two methods to iterate over an object which are discussed below: Method 1: Using for…in loop: The properties of the object can be iterated over using a for..in loop. This loop is used to iterate over all non-Symbol iterable properties of an object.

How do you iterate over the properties of an object and its children objects?

Object. entries() is the recommended method for iterating over an object's properties in JavaScript. Since the method returns a multidimensional array, we can greatly simplify our code by using the array destructuring syntax to retrieve each property into a separate variable.

Which iterator do we use to iterate over the properties of objects?

If you would like to iterate directly over the values of the keys of an object, you can define an iterator , just like JavaScipts's default iterators for strings, arrays, typed arrays, Map and Set. JS will attempt to iterate via the default iterator property, which must be defined as Symbol. iterator .


3 Answers

Recursion is your friend:

function iterate(obj) {
    for (var property in obj) {
        if (obj.hasOwnProperty(property)) {
            if (typeof obj[property] == "object")
                iterate(obj[property]);
            else
                console.log(property + "   " + obj[property]);
        }
    }
}

Note: don't forget to declare property locally using var!

like image 107
Marcel Korpel Avatar answered Sep 21 '22 19:09

Marcel Korpel


That's great anwsers, although the array cases is not covered, here's my contribution:

var getProps = function (obj) {
    for (var property in obj) {
        if (obj.hasOwnProperty(property) && obj[property] != null) {
            if (obj[property].constructor == Object) {
                getProps(obj[property]);
            } else if (obj[property].constructor == Array) {
                for (var i = 0; i < obj[property].length; i++) {
                    getProps(obj[property][i]);
                }
            } else {
                console.log(obj[property]);
            }
        }
    }
}
getProps(myObject);
like image 26
domi Avatar answered Sep 21 '22 19:09

domi


To simply display the object structure, I often use: console.log (JSON.stringify (obj))

like image 29
HBP Avatar answered Sep 21 '22 19:09

HBP