Question:
How do you add elements to a multidimensional array in javascript?
anonymous
2012-03-15 17:28:20 UTC
Is there a way to add elements to multidimensional arrays on the fly? Such as something like this that obviously throws an error


var myArray = new Array();

for(i=0;i<5;i++){
myArray[i][0] = "Something";
myArray[i][1] = "Something else";
}
Four answers:
x8oi
2012-03-15 17:35:58 UTC
Think of multidimensional arrays in JavaScript as if they are arrays of arrays (in fact, that's exactly what they are).



So



var myArray = [ [5, 6, 7], [4, 3, 2] ];



myArray[1][2]; // returns 2

myArray[0][0]; // returns 5

myArray[0].push(8); // adds 8 to the first array



This means that you have to implicitly create each "column" as it were.
?
2016-12-18 10:33:23 UTC
Multidimensional Array Javascript
house
2016-11-09 10:35:04 UTC
the .length components of the array tells you strategies many products are interior the array. confirm that's no longer a 2nd array or you will get exciting consequences. I forget approximately what the tactic of dealing with a multidimensional array replaced into. i think of I dealt with it by technique of consistently making specific I never had greater desirable than a 1D array. yet i got here upon a source that had an thought the type you need to calculate the dimensions of a multidimensional array, although that's grotesque. javascript is quite purely made for 1D arrays. case in point, var enjoying cards = new Array(a million, 2, 3, 4, 5, 6, 9); enjoying cards.push(10, a million, a million, 2); //upload 4 new enjoying cards enjoying cards[]=7; //upload new card (i think of) var i, result=0; for (i=0; i < a.length; i++) { result+=enjoying cards[i]; } //result's composed of the sum of the enjoying cards.
JerryOfBorg
2012-03-15 17:33:48 UTC
Try this link:



http://www.kavoir.com/2009/02/javascript-multi-dimensional-array.html


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...