How to push array into another array as one element

Viewed 25354

I'm new in JS, can't find solution to do something like that

var arr = [0];
var elem = [1, 2, 3];
???
console.log(arr); // shows [0, [1, 2, 3]];

I've tried with .push(elem), JS decides that I passed array of values (not a single one), and concatenate content of arr and elem arrays, so that the result is [0, 1, 2, 3]

3 Answers

You may try to use spread operator to concatenate values of an array.
For example:

let arr1 = [1, 2, 3];
let arr2 = [...arr1, 4, 5];

console.log(arr2);
//Output: [ 1, 2, 3, 4, 5 ]
Related