How do you put properties in a multidimensional-jagged array


#1

my_array = [ ];

my_array[0] = "parrot"; I can put elements this way and can make it like this: my_array =["parrot"];

But how do I put elements/properties in a multi-dimensional array.

I want to put/access values in an empty array and want to make it like this for example:

my_array = [[1,2,3], [ [ 4,5,6] ], "blackcap"];


#2

you where so close here:

my_array[0] = "parrot";

all you had to do, was put in an array instead of a string:

my_array[0] = [1,2,3];

i am not sure how to explain this better, you just put an array in the array, just like you would insert a string, except you insert an array


#3

I was looking for something so that I do not need to write like:

my_array [0] = [ [1,2,3], [4], [5,6,7]];

like (the code below might be a different thing I just saying it could belike this):
my_array = [];

my_array[0] = 2;
my_array[[0,1]] = 3;
my_array[[0,2]] = 4;
my_array[1] = "cat";

my_array [[1,0]] = 9;
console.log(my_array);

So that I can really construct a jagged multidimensional array with indexing. Anyway I think for the moment, I can skip this. But, thanks for your reply.


#4

There isn't a two-dimensional array type or syntactic sugar, but what you can do is put arrays in your array. So that means that you use one index to get at the row, and then another to get at the column.

my_2D_array[3][2] = 3;  // 4th row, 3rd column

third_row = my_2D_array[2];
second_col_of_third_row = third_row[1];

#5

but you can:

my_array = [];
my_array[0] = [2];
my_array[0][1] = 3;
my_array[0][2] = 4;
my_array[1] = [5];
my_array[1][1] = 6;
my_array[1][2] = 7;
console.log(my_array);

You need to push an array into my_array, once you did this, you can add items to your nested array, although i would do this in one go:

my_array = [];
my_array[0] = [2,3,4];
my_array[1] = [5,6,7];
console.log(my_array);

which is much easier and faster.


#6

@ionatan , @stetim94 Thanks, I was looking for something like this.