The Wayback Machine - https://web.archive.org/web/20240617105308/https://www.geeksforgeeks.org/copy-array-items-into-another-array-in-javascript/
Open In App

Copy Array Items into Another Array in JavaScript

Last Updated : 07 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

We will see how to copy array items into another array in JavaScript. This operation is valuable when you need to generate a new array containing either identical or a subset of elements from an original array.

These are the following ways to solve this problem:

Approaches 1: Using the Spread Operator (…)

The spread operator (…) is a concise syntax introduced in ES6 that allows us to expand iterable objects, like arrays, into individual elements. To copy an array using the spread operator, you simply spread the elements of the original array into a new array literal.

Syntax:

const copiedArray = [...originalArray];

Example: In this example, we will see the use of the spread operator.

Javascript
const originalArray = [1, 2, 3, 4, 5];
const copiedArray = [...originalArray];

// Output: [1, 2, 3, 4, 5]
console.log(copiedArray); 

Output
[ 1, 2, 3, 4, 5 ]

Approach 2: Using slice() Method

The slice() method is an array method in JavaScript that returns a shallow copy of a portion of an array into a new array. When called without any arguments, it can be used to copy the entire array.

Syntax:

const copiedArray = originalArray.slice();

Example: In this example, we will see the use of the slice() Method.

Javascript
const originalArray = [1, 2, 3, 4, 5];
const copiedArray = originalArray.slice();

// Output: [1, 2, 3, 4, 5]
console.log(copiedArray); 

Output
[ 1, 2, 3, 4, 5 ]

Approach 3: Using apply() Method

The apply() method is an older approach to copying an array, but it is less commonly used compared to the spread operator and slice() method. The apply() method calls a function with a given value and an array of arguments. To copy an array using apply(), you can use the concat() method along with apply() to concatenate the elements of the original array into a new array.

Syntax:

const copiedArray = Array.apply(null, originalArray);

Example: In this example, we will see the use of apply() Method.

Javascript
const originalArray = [1, 2, 3, 4, 5];
const copiedArray = Array.apply(null, originalArray);

// Output: [1, 2, 3, 4, 5]
console.log(copiedArray); 

Output
[ 1, 2, 3, 4, 5 ]

Approach 4: Using Array.concat() Method

This approach concatenates the source array with an empty array using the Array.prototype.concat() method, which returns a new array with all the elements.

Syntax:

const newArray = [].concat(sourceArray);

Example: In this example, we will see the use of Array.concat() Method.

Javascript
const sourceArray = [1, 2, 3, 4, 5];
const newArray = [].concat(sourceArray);

console.log(newArray);

Output
[ 1, 2, 3, 4, 5 ]

Approach 5: Using push() Method

In JavaScript, to copy array items into another array using the push method, iterate over the given array and use push to add each item to our result array

Syntax:

arr.push(element0, element1, â€Ķ , elementN)

Example: In this example, we use the push() method to copy array items into another array.

Javascript
// Source array
let array1 = [10, 20, 30, 40, 50];
let result = [];

// Copy items from array1 to result using push()
for (let i = 0; i < array1.length; i++) {
    result.push(array1[i]);
}

console.log(result);

Output
[ 10, 20, 30, 40, 50 ]

Approach 6: Using map() Method

JavaScript map method can be used to copy array items into another array. By applying a callback function that returns the original item, a new array is created with copied items,

Syntax:

map(function (element) { /* â€Ķ */ })

Example: In this example, we are using the map() method to copy an array item into another array.

Javascript
const arr = [5, 10, 15, 20, 25];
const result = arr.map(item => item);

console.log(result);

Output
[ 5, 10, 15, 20, 25 ]


Previous Article
Next Article

Similar Reads

Copy Elements of One Array into Another Array in PHP
Copying elements from one array to another is a common operation in PHP. Copy elements operation is used to create a duplicate, merge arrays, or extract specific elements. In this article, we will explore various approaches to copying elements from one array into another in PHP. Table of Content Using Assignment OperatorUsing foreach LoopUsing Spre
2 min read
How to copy the content of a div into another div using jQuery ?
Given an HTML document containing some div element and the task is to copy a div content into another div as its child using jQuery. There are two approaches to solve this problem that are discussed below: Approach 1: First, select the div element which needs to be copied into another div element.Select the target element where the div element is c
2 min read
What is shallow copy and deep copy in JavaScript ?
JavaScript is a high-level, dynamically typed client-side scripting language. JavaScript adds functionality to static HTML pages. Like most other programming languages JavaScript allows supports the concept of deep copy and shallow copy.  Shallow CopyWhen a reference variable is copied into a new reference variable using the assignment operator, a
4 min read
Angular PrimeNG Carousel Items Per Page and Scroll Items
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. This article will show us how to use Carousel in Angular PrimeNG. Angular PrimeNG Basic Carousel is used to render the basic carousel. The carousel is
4 min read
Copy all the attributes of one element and apply them to another with JavaScript
The task is to copy all attributes of one element and apply them to another with the help of JavaScript. Here 2 approaches are discussed. Approach 1: Select both elements(target and source element).Get the properties of source element by using el.prop('attributes') method.Use .each() method on each object property and set that property to the targe
2 min read
Copy the entire contents of a directory to another directory in PHP
Given a directory and the task is to copy the content of the directory to another directory using PHP functions. There are many functions used to copy the content of one directory to another. Used Functions: copy() Function: The copy() function is used to make a copy of a specified file. It makes a copy of the source file to the destination file an
3 min read
How to copy a file from one directory to another using PHP ?
The copy() function in PHP is used to copy a file from source to target or destination directory. It makes a copy of the source file to the destination file and if the destination file already exists, it gets overwritten. The copy() function returns true on success and false on failure. Syntax: bool copy( string $source, string $destination, resour
2 min read
How to copy properties from one object to another in ES6 ?
The properties of one object can be copied into another object through different methods. Here are some of those methods. Object.assign(): By using the Object.assign() method, all enumerable properties of one or more source objects are copied to the target object. This method returns the modified target object. The properties of the target object a
3 min read
How to Add Image into Dropdown List for each items ?
Programmers can add images into the dropdown list for each items by two methods which are mentioned below. Method 1: Using CSS The .dropdown class uses position: relative; is used when the user needs the content to be set right under the dropdown button (It is done using position: absolute). The .dropdown-content class holds the actual dropdown con
3 min read
Javascript Program To Merge A Linked List Into Another Linked List At Alternate Positions
Given two linked lists, insert nodes of the second list into the first list at alternate positions of the first list. For example, if first list is 5-&gt;7-&gt;17-&gt;13-&gt;11 and second is 12-&gt;10-&gt;2-&gt;4-&gt;6, the first list should become 5-&gt;12-&gt;7-&gt;10-&gt;17-&gt;2-&gt;13-&gt;4-&gt;11-&gt;6 and second list should become empty. The
3 min read