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.
+ There are no comments
Add yours