How To Copy Array Items into Another Array in JavaScript – Definitive Guide

There are different methods to copy array items into another array in JavaScript.

Quick Answer: array1.push(…array2);

This tutorial explains the different methods to copy array items into another array and when it is appropriate to use each technique.

Copy Array Items into Another Array Using Array Push() And Spread Operator (ES6 Syntax)

The array push() method pushes an element into an array.

To copy the array items into another array,

  • Invoke the push method from the target array
  • Pass the array elements by expanding them using the spread [...] operator.

With this, all the elements will be pushed to the target array.

Use this method when you want to push all elements from an array into an existing array, and the array is not huge.

Code

The following code demonstrates how to push the elements from the newNumArray into the array called numArray.

var numArray = [1, 2, 3];

var newNumArray = [4, 5];

numArray.push(...newNumArray);

console.log(numArray);

Output

[1, 2, 3, 4, 5]

Copy Array Items into Another Array Using Concat()

The concat() method merges two or more arrays and creates a new array object out of it.

To copy array items into another array,

  • Invoke the concat() method in the first array and pass the second array as a parameter
  • The elements from both arrays are merged, and a new array will be created.

Use this method when you want to create a new array by copying array elements into it.

Code

The following code merges the numArray and the newNumArray and creates a new array.

var numArray = [1, 2, 3];

var newNumArray = [4, 5];

var consolidatedArray = numArray.concat(newNumArray);

console.log(consolidatedArray);

Output

[1, 2, 3, 4, 5]

Copy Array Items into Another Array Using Slice()

The slice() method returns a shallow copy of a specific portion of the array into a new array object.

  • To copy all array elements into another array, invoke the slice() method without any parameters
  • To copy specific items from an array into another array, pass the startindex and endindex to the slice method. The startindex item is inclusive, and the endindex item is exclusive.

Use the slice method to copy a specific portion of items into a new array.

Code

The following code demonstrates how to copy items with index 0 until 2.

var numArray = [1, 2, 3, 4, 5];

var newNumArray = numArray.slice(0, 2);

console.log(newNumArray);

Output

[1, 2]

Copy Array Items into Another Array Using Map Function

The map() method applies the defined callback function to every element of the array and returns a new array with the result.

Use this method when you want to copy array items into another array after applying a specific function to the elements in the array—for example, calculating the square of the numbers or converting the words into the upper case or lower case. You can also use it when the resultant array doesn’t exist. The map method will create an array and copy the item from the existing array.

Code

The following code demonstrates how to calculate the square of the numbers in an array and copy it into the new array.

var numArray = [1, 2, 3];

const newNumArray = numArray.map(obj => (obj*obj));

console.log(newNumArray);

Output

[1, 4, 9]

Additional Resources

Leave a Comment