Reputation: 149
I used another (my) way to store the elements of some array in another as a spread method. I used join method for this way, but the array contains only one. Here's my code:
const arr = [1, 2, 3];
const newArray = [eval(arr.join(', ')), 4, 5]
console.log(newArray) // [3, 4, 5]
Upvotes: 1
Views: 60
Reputation: 18096
You can use concat
for that.
The concat()
method will join two (or more) arrays and will not change the existing arrays, but instead will return a new array, containing the values of the joined arrays.
const arr = [1, 2, 3];
const newArray = arr.concat([4, 5]);
console.log(newArray)
Another option is to use spread syntax (...) (introduced in ES6)
const arr = [1, 2, 3];
const newArray = [...arr, ...[4, 5]];
console.log(newArray)
Upvotes: 1
Reputation: 20354
Try this:
const arr = [1, 2, 3];
const newArray = [...arr, 4, 5];
console.log(newArray);
Upvotes: 1