1

I have the following object:

myObject: {

 myArray1: [1,2,3],
 myArray2: [4,5,6],
 myArray3: [7,8,9]
}

This is an object that keeps growing in Arrays(dynamic array?). So I need to figure out a method to access it. I came across using a for( var key in myObject) with something like this:

    for (var key in myObject) {
     var obj = myObject[key];
       for (var prop in obj) {
           //thinking that this will print the first value of the array
       console.log(prop[0]);
     }
    }

but it doesn't work it prints undefined. I know using a for in is not the way to access an object correctly. I'm wondering if anyone could suggest a method to access the values of this object through a loop.

Thanks!

7
  • 1
    possible duplicate of Access / process (nested) objects, arrays or JSON Commented Apr 16, 2014 at 3:36
  • Why do you think that for...in works differently for objects than it works for arrays? Commented Apr 16, 2014 at 3:36
  • @FelixKling This is very interesting. I thought for some reason it wasn't recommended. I don't remember where I read it. Commented Apr 16, 2014 at 3:41
  • Well, yeah, you shouldn't use for..in for arrays, but that doesn't mean that it works differently for arrays than it does for objects. Commented Apr 16, 2014 at 3:55
  • @FeliKling It makes sense now for me. Your link is very helpful in how to use for in when the data is unknown Commented Apr 16, 2014 at 4:04

3 Answers 3

1

Iterating an object with for..in is okay, but not an array. Because when you sue for..in with an array, it will not get the array values, but the array indices. So, you should be doing something like this

for (var key in myObject) {
    var currentArray = myObject[key];
    for(var i = 0; i < currentArray.length; i += 1) {
        console.log(currentArray[i]);
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

This works. And I guess this is the right way to do it??
@MauricioSanchez Indeed, this is how arrays have to iterated (or Array.forEach). NEVER use for..in with Arrays.
Although the method that vivek_nk suggested, it worked as well. I guess it is a matter of better coding practices??
@MauricioSanchez Well, if you really know what you are doing, that is fine. But don't use for..in with arrays.
@MauricioSanchez Also read this section from the MDN Docs. It says why you should not use that method.
1

You made a mistake in 2nd loop. obj is the array and prop is the index

for (var key in myObject) {
  var obj = myObject[key];
   for (var prop in obj) {
       //this will print the first value of the array
   console.log(obj[prop]); //obj is the array and prop is the index
 }
} 

Comments

1

prop is the index of the array, its not the array. obj is the array. So it should be:

console.log(obj[prop]); 

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.