1

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

6
  • 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. Commented Dec 21, 2013 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? Commented Dec 21, 2013 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. Commented Dec 21, 2013 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. Commented Dec 21, 2013 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. Commented Dec 21, 2013 at 18:05

4 Answers 4

1

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

1 Comment

That's not 100% correct. You can use a for...in loop (after all, arrays are objects too) but you shouldn't.
1

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

2 Comments

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++) { } }
for in loops should typically be used when iterating over objects keys, hence the for key in name
1

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

4 Comments

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++) { } } ?
Array elements can only be accessed with numeric index. You can use keys with js objects. Here is an example
@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 I guess that's why there's Coffeescript. I still have to learn JS though.
0

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
}

1 Comment

The only problem is that there's no inner for loop to print out other elements of the subarray

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.