Declare a two-dimensional array in javascript with no position number specified

Asked

Viewed 236 times

-1

As you create a two-dimensional array in javascript where you don’t need to put a specific size, and it keeps growing as I add things.

1 answer

1

There are no multidimensional arrays in javascript, there are nested arrays (Jagged array), which are arrays within arrays.

You do not need to specify the size of the array, even if you specify a size with: var arr = new Array(2); can add new element to the array.

You can create nested arrays this way:

var jaggedArray = [];

var array1d_1 = ["maria", "joana"];

var array1d_2 = ["rita", "lena", "leonor"];

jaggedArray.push(array1d_1, array1d_2);

console.log(jaggedArray);

can even set an array1d to the respective index:

jaggedArray[0] = array1d_1;
jaggedArray[1] = array1d_2;

// para ter acesso
console.log(jaggedArray[1][2]); // leonor

Browser other questions tagged

You are not signed in. Login or sign up in order to post.