0
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?

3
  • 1
    Using integer for Object keys is terrible practice - don't use it! Either use array or use string for keys! Commented May 28, 2015 at 14:56
  • it's object thorugh objects not arrays. Commented May 28, 2015 at 14:59
  • 1
    when you use a for...in loop be sure to use the var keyword otherwise you are making your variable i a global variable. e.g. for (var i in mainobject){..} Commented May 28, 2015 at 15:05

2 Answers 2

1

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]);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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]);
   };
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.