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!

mauricioSanchez
  • 366
  • 10
  • 23
  • 1
    possible duplicate of [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Felix Kling Apr 16 '14 at 03:36
  • Why do you think that `for...in` works differently for objects than it works for arrays? – Felix Kling Apr 16 '14 at 03:36
  • @FelixKling This is very interesting. I thought for some reason it wasn't recommended. I don't remember where I read it. – mauricioSanchez Apr 16 '14 at 03: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. – Felix Kling Apr 16 '14 at 03:55
  • @FeliKling It makes sense now for me. Your link is very helpful in how to use for in when the data is unknown – mauricioSanchez Apr 16 '14 at 04:04
  • @FeliKling I guess the question now is, if I want to access a certain part of that array when using obj[prop]. I will just do something like obj[1]. If I want to access the second element of the array for example? – mauricioSanchez Apr 16 '14 at 04:13
  • `obj[1]` would access the second element of the array, if that's what you wanted to know. Or you could directly write `myObject[key][1]`. – Felix Kling Apr 16 '14 at 04:17

3 Answers3

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]);
    }
}
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
  • This works. And I guess this is the right way to do it?? – mauricioSanchez Apr 16 '14 at 03:30
  • @MauricioSanchez Indeed, this is how arrays have to iterated (or `Array.forEach`). *NEVER* use `for..in` with Arrays. – thefourtheye Apr 16 '14 at 03:31
  • Although the method that vivek_nk suggested, it worked as well. I guess it is a matter of better coding practices?? – mauricioSanchez Apr 16 '14 at 03:32
  • @MauricioSanchez Well, if you really know what you are doing, that is fine. But don't use `for..in` with arrays. – thefourtheye Apr 16 '14 at 03:34
  • @MauricioSanchez Also read this [section from the MDN Docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in#Description). It says why you should not use that method. – thefourtheye Apr 16 '14 at 03:35
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
 }
} 
vivek_nk
  • 1,590
  • 16
  • 27
1

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

console.log(obj[prop]); 
juvian
  • 15,875
  • 2
  • 37
  • 38