Copy Array Items Into Another Array in JavaScript
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 Array
Below 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 spread syntax (…) is used to create a shallow copy of the a1.
- This method copies all elements from a1 into a2, resulting in [1, 2, 3, 4, 5] being logged to the console.
- Any changes made to a2 will not affect a1, as the two are separate arrays.
2. Using slice() Method
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 slice() method is used to create a shallow copy of the a1. It copies all elements from index 0 to the end of the array by default.
- The a2 is a new array with the same elements as a1, and the output is [1, 2, 3, 4, 5]. Any modifications to a2 will not affect the original array.
3. Using Array.concat() Method
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:
- The concat() method is used to create a shallow copy of the a1 array by concatenating it with an empty array [].
- The a2 will be a new array containing the same elements as a1, resulting in [1, 2, 3, 4, 5]. Modifying a2 will not affect a1.
4. Using map() Method
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);
Output[ 5, 10, 15, 20, 25 ]
In this example:
- The map() method creates a new array by applying the provided function (item => item) to each element in the a1 array, effectively returning a copy of the original array.
- The a2 array will be the same as a1, resulting in [5, 10, 15, 20, 25]. The operation does not modify the original array.
5. Using Array.from() Method
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:
- The Array.from() method creates a shallow copy of the a1 array by converting it into a new array.
- The a2 will have the same elements as a1, resulting in [1, 2, 3, 4, 5].
Deep Copy Arrays
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 Methods
JavaScript’s JSON.stringify()
and JSON.parse()
methods can be used to create a deep copy by serializing and deserializing the array:
JavaScript
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.- This creates a deep copy because all nested objects are also copied.
Why Copy Arrays?
In JavaScript, there are a few common reasons to copy an array:
- Avoiding Changes to the Original: If you modify an array directly, it might accidentally change parts of your code that depend on it. Copying the array ensures the original stays the same.
- Creating a Duplicate: Sometimes, you need a copy of the array to work with, without affecting the original. This is useful when you want to experiment or modify the data temporarily.
- Manipulating Data: Copying arrays is helpful when you want to change or process the data but keep the original array untouched. This way, you don’t lose the original information.
Conclusion
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.
Similar Reads
How to Add Elements to a JavaScript Array?
Here are different ways to add elements to an array in JavaScript. 1. Using push() MethodThe push() method adds one or more elements to the end of an array and returns the new length of the array. Syntax array.push( element1, element2, . . ., elementN );[GFGTABS] JavaScript const arr = [10, 20, 30,
3 min read
Create an Array of Given Size in JavaScript
The basic method to create an array is by using the Array constructor. We can initialize an array of certain length just by passing a single integer argument to the JavaScript array constructor. This will create an array of the given size with undefined values. Syntax cosnt arr = new Array( length )
4 min read
Insert at the Beginning of an Array in JavaScript
Following are different ways to add new elements at the beginning of an array 1. Using the Array unshift() Method - Most Used:Adding new elements at the beginning of the existing array can be done by using the Array unshift() method. This method is similar to the push() method but it adds an element
2 min read
Remove Elements From a JavaScript Array
Here are the various methods to remove elements from a JavaScript Array 1. Using pop() methodThe pop() method removes and returns the last element of an array. This function decreases the length of the array by 1 every time the element is removed. [GFGTABS] javascript let a = ["Apple",
6 min read
Remove Duplicate Elements from JavaScript Array
To Remove the elements from an array we can use the JavaScript set method. Removing duplicate elements requires checking if the element is present more than one time in the array. 1. Using JavaScript Set() - Mostly UsedThe JavaScript Set() method creates an object containing only unique values. To r
3 min read
How to Remove Multiple Elements from Array in JavaScript?
Here are various methods to remove multiple elements from an array in JavaScript 1. Using filter() MethodThe filter() method creates a new array with the elements that pass the condition. This method does not change the original array. [GFGTABS] JavaScript let a = [1, 2, 3, 4, 5]; let remove = [2, 4
4 min read
Insert at the Beginning of an Array in JavaScript
Following are different ways to add new elements at the beginning of an array 1. Using the Array unshift() Method - Most Used:Adding new elements at the beginning of the existing array can be done by using the Array unshift() method. This method is similar to the push() method but it adds an element
2 min read
Reverse an Array in JavaScript
Here are the different methods to reverse an array in JavaScript 1. Using the reverse() MethodJavaScript provides a built-in array method called reverse() that reverses the elements of the array in place. This method mutates the original array and returns the reversed array. [GFGTABS] JavaScript let
3 min read
JavaScript - Delete last Occurrence from JS Array
These are the following ways to remove the last Item from the given array: 1. Using pop() Method (Simple and Easiest Method for Any Array) The pop() method is used to get the last element of the given array it can also be used to remove the last element from the given array. [GFGTABS] JavaScript let
2 min read
Remove Empty Elements from an Array in JavaScript
Here are different approaches to remove empty elements from an Array in JavaScript. 1. Using array.filter() MethodThe array filter() method is used to create a new array from a given array consisting of elements that satisfy given conditions. array.filter( callback( element, index, arr ), thisValue
3 min read