How to push to the multidimensional array with JavaScript?

Estimated read time 1 min read

To push an element to a multidimensional array in JavaScript, you can access the last array in the multi-dimensional array and use the Array.push() method to add the new element.

Here’s an example:

let array = [[1, 2], [3, 4]];
array[array.length - 1].push(5);
console.log(array); // [[1, 2], [3, 4, 5]]

In this example, we create a multidimensional array array with two arrays inside it. To add an element to the last inner array, we use the expression array[array.length - 1] to access the last inner array, and then use the Array.push() method to add the new element 5. The resulting array is then logged to the console.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply