Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I don't understand how to find the length of a subarray in Javascript. Here is an example from an exercise:

 var table = [
["Person",  "Age",  "City"],
["Sue",     22,     "San Francisco"],
["Joe",     45,     "Halifax"]
];

I have tried to print out the elements of the sub-arrays individually using these for loops:

for(person in table) {
    for(var i = 0; i < table[person].length; i++);
        console.log(table[person][i]);
}

but it seems that

table[person].length

is not valid syntax although

table.length 

is valid and

table[person][i]

returns the element at the sub-index table_person_i

share|improve this question
    
I can assure you that table[person].length is valid syntax. Why do you iterate over the array with a for...in loop? Using a for loop might fix your problem. – Felix Kling Dec 21 '13 at 17:55
    
I prefer the for ... in syntax when iterating over arrays. I'm doing an exercise on Codecademy and it's rejecting my syntax. It's giving 'undefined' for each row. Maybe this is an issue with their interpreter? – Chris Ochsenreither Dec 21 '13 at 17:57
1  
table[person].length would be valid, if you had an array named person in the object called table. But that is not the structure you have - the structure described above is kind of a misunderstanding of arrays, or at least a weird way to use them. – JAL Dec 21 '13 at 18:00
1  
Well, there are reasons why you should not use for...in for arrays. See developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…. And only because Codecademy doesn't like your code doesn't mean it is syntactically invalid. – Felix Kling Dec 21 '13 at 18:04
1  
@JAL: You are confusing table[person] with table['person']. person is a variable containing values such as "0", "1", etc. So you end up doing table["0"].length, exactly matches the structure. It gets the length of the first element in table. – Felix Kling Dec 21 '13 at 18:05

4 Answers 4

up vote 1 down vote accepted

That's an array, not an object, so you can't use for/ in loops. Use the regular for loop instead.

//for (person in table) {

for (var person = 1; person < table.length; person++) {
    for(var i = 0; i < table[person].length; i++)
    {
        console.log(table[person][i]);
    }
}
share|improve this answer
    
That's not 100% correct. You can use a for...in loop (after all, arrays are objects too) but you shouldn't. – Felix Kling Dec 21 '13 at 21:47

Try this:

for (var j = 0; j<table.length; j++) 
{
     //j(th) element of table array
     for (var i = 0; i < table[j].length; i++)
     {
         //i(th) element of j(th) element array
         console.log(table[j][i]);
     }
}
share|improve this answer
    
thanks. that works, but is there a way to do it with a for(key in array) { for (i = 0; t < array[key].length; i++) { } } ? – Chris Ochsenreither Dec 21 '13 at 18:02
1  
Array elements can only be accessed with numeric index. You can use keys with js objects. Here is an example – Kaf Dec 21 '13 at 18:07
    
@ChrisOchsenreither You should really avoid using for-in on arrays, that will unleash demons...and unreliable data (i.e iterating over other properties of the array object which you aren't expecting). – Lior Dec 21 '13 at 18:30
    
@Lior I guess that's why there's Coffeescript. I still have to learn JS though. – Chris Ochsenreither Dec 21 '13 at 18:33

In your example, your array is an array of array. To fetch person's names, and according your example:

for (var i = 1; i < table.length; i++)
{
 console.log(table[i][0]); // first element of each sub array
}
share|improve this answer
    
The only problem is that there's no inner for loop to print out other elements of the subarray – Chris Ochsenreither Dec 21 '13 at 18:45

You should use nested for loops for this task:

for (var i = 0; i < table.length; i++) {
    for (var j = 0; j < table[i].length; j++) {
        console.log(table[i][j]);
    }
}
share|improve this answer
    
thanks. that works, but is there a way to do it with a for(key in array) { for (i = 0; t < array[key].length; i++) { } } – Chris Ochsenreither Dec 21 '13 at 18:02
1  
for in loops should typically be used when iterating over objects keys, hence the for key in name – tymeJV Dec 21 '13 at 18:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.