Ratiess
Ratiess

Reputation: 59

iterate through objects javascript with Int index

Object {0: Object, 1: Object}
    0:Object
        0: "aaa"
        1: "bbb"
    1:Object
        0: "ccc"
        1: "ddd"



for (i in mainobject){
            for (l in i){

                        console.log("l is: "+ l["1"]);

            }
}

How i get "ddd" in javascript, the loop i have only return index or undefined?

Upvotes: 0

Views: 104

Answers (2)

dimitar veselinov
dimitar veselinov

Reputation: 138

That i you get gives you the index rather than the nested object. You need to adjust your second for loop.

for(var i in mainobject) {
   var secondobject = mainobject[i];
   for(var l in secondobject) {
      console.log("l is: "+ secondobject[l]);
   };
};

Upvotes: 0

Alex McMillan
Alex McMillan

Reputation: 17952

for..in loops in javascript put the KEY into the iterator variable you create, not the value. Try this:

for (var i in mainObject) {
    var item = mainObject[i];
}

If each of these objects is a nested object you want to inspect, do so:

for (var i in mainObject) {
    var item = mainObject[i];
    for (var j in item) {
        console.log(item[j]);
    }
}

Upvotes: 1

Related Questions