2

I would like to know how to create an array, and add elements to it with a nested for-loop. How do I declare the array? I want the finished array to look like this:

var array2d = {
[0, 1, 2, 3],
[0, 1, 2, 3],
[0, 1, 2, 3],
[0, 1, 2, 3],
};

I don't want to make the inner arrays formatted as objects. I want a simple array which I can access like this:

var element = array2d[2][1]; //returns '1'
JJJ
  • 32,902
  • 20
  • 89
  • 102

1 Answers1

1

What John Hartsock probably meant (in his answer) was:

var my2DArray = [], i, j;

for(i = 0; i < 5; i++){
  my2DArray.push([]);
  for(j = 0; j < 5; j++){
    my2DArray[i].push("Array Position " + i + ", " + j);
  }
}

alert(my2DArray[0][0]); //will alert "Array Postion 0, 0"
alert(my2DArray[1][4]); //will alert "Array Postion 1, 4"

Working jsfiddle demo here.

OR using a temporary reference to each new 'sub-array' (can be faster):

var my2DArray = [], i, j, tempRef;

for(i = 0; i < 5; i++){
  tempRef = my2DArray[i] = [];
  for(j = 0; j < 5; j++){
    tempRef.push("Array Position " + i + ", " + j);
  } 
}

alert(my2DArray[0][0]); //will alert "Array Postion 0, 0"
alert(my2DArray[1][4]); //will alert "Array Postion 1, 4"

Working jsfiddle demo here.

Explanation:

One normally uses var arr=[]; to create an array and arr.push(/*content*/); to add a new array-item to the end of the array (so it is essentially the same as arr[arr.length]=/*content*/;).

Thus, if you have an array (my2DArray), then you add new arrays (since arrays are passed by reference, not copied) to your main/root array: my2DArray.push([]); to make my2DArray[my2DArray.length] reference this new array (and once this array exists, you can do stuff with it: my2DArray[index][sub_index]).

So, say you have just one loop, then you could also add a complete new array with a single push: my2DArray.push([0,1,2,3]);

Put differently, (using what is explained above,) this single line will create exactly the 2d array you gave as example in your question:

for(var array2d=[], i=0; i<4; array2d[i++]=[0, 1, 2, 3]);
alert(  array2d[2][1]  );  //returns '1'

jsfiddle demo here.

By the way, technically there aren't even arrays in javascript, just fancy objects with some special features (excellent explanation here: https://stackoverflow.com/a/2891043/588079)

Community
  • 1
  • 1
GitaarLAB
  • 14,536
  • 11
  • 60
  • 80
  • While the last comment about arrays being "fancy objects" is relevant, it should still be pointed out that some distinction is made due to the global `Array` constructor existing as a separate entity from `Object`. – ajp15243 Dec 21 '13 at 05:27
  • wtf ?!? adding url's changes the last array-index of the last line of each code-block – GitaarLAB Dec 21 '13 at 05:30
  • @ajp15243: About your suggested edit: I used `/*content*/` so I wouldn't have to explain [objects, wrappers, values, primitives etc](http://stackoverflow.com/a/16115543/588079). Also 'JavaScript' (note casing) refers to an engine implementation, so I specifically use 'javascript' (when I mean ES). As to your comment, you are right, that's why I pointed to an excellent answer on SO. But *thank you* for caring! – GitaarLAB Dec 21 '13 at 05:41
  • Fair enough, and you're welcome! – ajp15243 Dec 21 '13 at 17:14