EN
JavaScript - copy array items into another array
0
points
In this article, we would like to show you how to copy array items into another array in JavaScript.
Quick solution:
// ONLINE-RUNNER:browser;
let array1 = ['a', 'b'];
let array2 = ['c', 'd'];
array1.push(...array2);
console.log(array1); // [ 'a', 'b', 'c', 'd' ]
console.log(array2); // [ 'c', 'd' ]
Alternative solution
In this example, we present an alternative solution of how to copy items from array2 into array1.
// ONLINE-RUNNER:browser;
let array1 = ['a', 'b'];
let array2 = ['c', 'd'];
array1 = array1.concat(array2);
console.log(array1); // [ 'a', 'b', 'c', 'd' ]
console.log(array2); // [ 'c', 'd' ]
Note:
If you want to add two arrays and save the result inside a new array go to this article.