How to push to a JavaScript multidimensional array?

Estimated read time 1 min read

In JavaScript, you can push elements to a multidimensional array by accessing the inner array and calling the push() method on it. Here’s an example:

let arr = [[1, 2], [3, 4]];
let innerArray = arr[0];
innerArray.push(3);

console.log(arr);

This will output [[1, 2, 3], [3, 4]].

If you want to push an element to a specific inner array, you can use the index of that array:

let arr = [[1, 2], [3, 4]];
let innerArrayIndex = 1;
arr[innerArrayIndex].push(5);

console.log(arr);

This will output [[1, 2], [3, 4, 5]].

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply