0

I've come across writing a piece of code where I wanted to reference the 2D array d2_arr[][] in a loop like so.

for (var i=0; i<d2_arr[i].length; i++) {
    //do something
}

Google Script compiler throws an error "cannot read length property from undefined". When I changed [i] for [1], it worked just fine. Could anyone please explain why this is wrong? And a related question: Can a 2D array have a different number of elements in a row? theoretically. An example would be:

[[1,2],[3,4,5],[6,7,8,9],[10,11]]

EDIT. Full code part.

var ids = []; 
 var j = 0; 
  for (var i=0; i<d2_arr[i].length; i++){
   if (d2_arr[i][2]<=0.05){
     ids[j]=d2_arr[i][0];
     j++;
   }
 }

I understood the mistake. Thank you!

2
  • I find the code sample very inadequate. If you want a full diagnosis of why you're getting the cannot read length property from undefined do you mind pasting the whole code (especially d2_arr)? Is it generated dynamically? Commented Nov 21, 2014 at 23:09
  • Yaw, I think I have mixed up things together. I posted the snippet of code to make it clearly for myself as well. From the answer I understood that I took the wrong length. This is my original code. In the EDIT I see that when I go through i as a row, I constrained it by the number of a row's length, which is wrong. Maybe there at some point the compiler threw an arrow due to an error in the array dimensions. Commented Nov 22, 2014 at 11:16

2 Answers 2

3

You typically need a nested for loop to traverse a 2-D array

    var d2_arr = [[1,2],[3,4,5],[6,7,8,9],[10,11]]
    
    for (var i=0; i<d2_arr.length; i++){
      for (var j=0; j<d2_arr[i].length; j++){
        console.log(d2_arr[i][j] + ' ')
      }
    }

It is perfectly fine for arrays to be "jagged" and contain uneven sized arrays in the main array.

Here is a fiddle http://jsfiddle.net/7Lr4542s/

Sign up to request clarification or add additional context in comments.

Comments

1

Arrays in JS can be of any size and any type. You can combine number and strings in array.

var twoDArray = [[1], ["one", "two"], ["i", "ii", "iii"]];
    for(var i = 0; i < twoDArray.length; i++) {
        for(var j = 0; j < twoDArray[i].length; j++) {
            print(twoDArray[i][j]);
        }
    }

    var threeDArray = [[["1.1.1", "1.1.2"], ["1.2.1", "1.2.2"]], [["1.2.1", "1.2.2"], ["1.2.1", "1.2.2"]], [["2.1.1", "2.1.2"], ["2.2.1", "2.2.2"]], [["2.2.1", "2.2.2"], ["2.2.1", "2.2.2"]]];
    for(var i = 0; i < threeDArray.length; i++) {
        for(var j = 0; j < threeDArray[i].length; j++) {
            for(var k = 0; k < threeDArray[i][j].length; k++) {
                print(twoDArray[i][j][k]);
            }
        }
    }

1 Comment

Thank you Naresh! I cannot vote for accepting two answers at the same time unfortunately, but your example of 3D array is something I was actually thinking if that's possible too.

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.