I have a 2d array that I created like such:
var images = new Array(50);
for(var i=0;i<=51;i++)
{
images[i]=new Array(2);
}
Now I would like to push values into this array, but I can’t get the proper syntax to work. How would I for example convert these statements:
images[1][0]='a';
images[1][1]='b';
images[1][2]='c';
into a push statement (regardless of position pushed into array)?
1 Like
n = images.length;
images.push(new Array());
images[n].push=‘a’;
images[n].push=‘b’;
images[n].push=‘c’;
1 Like
I was kind of hoping the result wouldn’t expand my code…
So it can’t be done in one line? You can’t push an array into an array?
1 Like
images.push(['a','b','c'])
1 Like
Stereofrog when I executed that code it worked (or at least it didn’t throw any errors), but my notation for retrieving elements, images[0], images[1], images[2] doesn’t return anything. In fact if I try to alert(images[1][0]) nothing pops up at all.
1 Like
var images = [];
images.push(["a", "b", "c"]);
images.push(["x", "y", "z"]);
alert(images[0][1]); // b
alert(images[1][0]); // x
1 Like
Ah, thanks. I guess it was creating a 3d array because I still had the for loop to fill the array with arrays.
1 Like