Last Updated : 28 Apr, 2025
In JavaScript, copying array items into another array in JavaScript is helpful when you want to make changes to the data without affecting the original array. It helps avoid mistakes, keeps the original data safe, and is useful when you need to work with a separate copy of the array for testing or processing.
Methods to Copy Array Items Into Another ArrayBelow are the following methods by which we can copy array items into another array:
1. Using the Spread Operator (...)The spread operator (...) is another modern and concise way to copy an array. It spreads the elements of the original array into a new array, creating a shallow copy.
JavaScript
const a1 = [1, 2, 3, 4, 5];
const a2 = [...a1];
console.log(a2);
In this example:
The slice() method is one of the simplest and most commonly used ways to copy an array. It creates a shallow copy of a portion of an array, and when used without any arguments, it copies the entire array.
JavaScript
const a1 = [1, 2, 3, 4, 5];
const a2 = a1.slice();
console.log(a2);
In this example:
The Array.concat() method in JavaScript copies array items into another array by merging the original array with one or more arrays or values.
JavaScript
const a1 = [1, 2, 3, 4, 5];
const a2 = [].concat(a1);
console.log(a2);
In this example:
If you need to copy an array and also apply some transformation to its elements, you can use map(). The map() method in JavaScript creates a new array by applying a function to each element of the original array.
JavaScript
const a1 = [5, 10, 15, 20, 25];
const a2 = a1.map(item => item);
console.log(a2);
[ 5, 10, 15, 20, 25 ]
In this example:
The Array.from() method creates a new array from an array-like object. It can also be used to copy an array, and you can optionally provide a map function to transform the elements.
JavaScript
const a1 = [1, 2, 3, 4, 5];
const a2 = Array.from(a1);
console.log(a2);
In this example:
All of the methods mentioned above create shallow copies of arrays. When the array contains objects, a shallow copy means the array structure is copied, but the objects inside the array will still be referenced by both the original and the new array. Here's an example:
JavaScript
const arr = [
{ name: 'mario', food: 'pizza' },
{ name: 'luigi', food: 'spaghetti' },
];
const arrCopy = [...arr];
console.log(arr === arrCopy);
console.log(arr[0] === arrCopy[0]);
To make a deep copy of arrays, where both the array and all the objects inside it are cloned, you can use the following methods:
JSON MethodsJavaScript's JSON.stringify()
and JSON.parse()
methods can be used to create a deep copy by serializing and deserializing the array:
const arr = [
{ name: 'mario', food: 'pizza' },
{ name: 'luigi', food: 'spaghetti' },
];
const arrCopy = JSON.parse(JSON.stringify(arr));
console.log(arr === arrCopy);
console.log(arr[0] === arrCopy[0]);
In this example:
JSON.stringify()
converts the array into a JSON string.JSON.parse()
parses the JSON string back into a new array.In JavaScript, there are a few common reasons to copy an array:
Copying arrays in JavaScript is a common task, and the method you choose depends on whether you need a shallow or deep copy. For shallow copies, the most straightforward options are the slice()
method, the spread operator, Array.from()
, and concat()
. For deep copies, using JSON.parse()
and JSON.stringify()
is a simple solution.
Copy Array Items into Another Array in JavaScript
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4