5 Ways To Clone an Array in Javascript

I am a developer based of Toronto, Canada. Currently working on learnly.dev to create a software development program that focuses on problem-solving. Software engineer @ Amazon.
Search for a command to run...

I am a developer based of Toronto, Canada. Currently working on learnly.dev to create a software development program that focuses on problem-solving. Software engineer @ Amazon.
No comments yet. Be the first to comment.
I've been building a platform where people can learn anything from. In order to improve my traffic, I was recommended by my friend, Rafeh, to start doing server-side rendered pages, essentially improving SEO. My current architecture is a React app, s...

The intent of this article is to share how people around the world in conflict zones are using education as a source of light. For a reason unknown to me, I am extremely lucky to have one of these students learning from my platform, Learnly. For all ...

Are you tired of computer science courses that jump straight into complex technologies without first teaching you the fundamentals of problem-solving? Do you want to learn the foundations of computer science and gain the skills you need to tackle any...

The last thing we want is to pay for something we don't like. I'm giving out 1-month free access to learnly. The problem with most online courses is that they focus too much on getting into syntax and technologies vs. first building the necessary sk...
Introduction Use case analysis is a technique used in software engineering to help determine what the stakeholders of the system want it to do and how they want it to behave. When planning a new application or making changes to an existing applicatio...

In JavaScript, arrays are passed by reference. Sometimes, we may want to create a copy of an array instead of passing the reference to the array — particularly if we're looping through an array, changing it in some way, and want a new one.
All the methods shown below do a shallow copy of an array. A shallow copy of an Array copies only the elements, whether they are reference types or value types. If the values of the array are references (objects), then those references in the new Array point to the same objects as those in the original Array.
Below we are going to review 5 different ways an array can be cloned.
A simple for loop can be used. We can use both, forEach function on the array and the for loop. I will demonstrate both.
Create an empty array and copy the elements one by one.
let numbers = [1, 2, 3, 4];
let numbersClone = [];
numbers.forEach(number => {
numbersClone.push(number);
});
console.log(numbersClone); // => [1, 2, 3, 4]
let numbers = [1, 2, 3, 4];
let numbersClone = [];
for (let i = 0; i < numbers.length; i++) {
numbersClone[i] = numbers[i];
// we could still use the push function on the array
// numbersClone.push(number);
}
console.log(numbersClone); // => [1, 2, 3, 4]
The slice function allows us to get a subset of the array. If we don't pass any parameters, it will create a shallow copy of the array.
let numbers = [1, 2, 3, 4];
// Review of getting only the subset
let subsetOfNumbers = numbers.slice(1, 3);
console.log(subsetOfNumbers); // => [2, 3]
// cloning the whole array
let numbersClone = numbers.slice();
console.log(numbersClone); // => [1, 2, 3, 4]
The concat function allows us to join an array with another array, the result is a new array without changing the original one. If we don't pass any parameters, it will create a copy of the array.
let numbers = [1, 2, 3, 4];
// example of joining another array
let numbersWithExtra = numbers.concat([5]);
console.log(numbersWithExtra); // => [1, 2, 3, 4, 5];
// concat without parameters simply clones
let numbersClone = numbers.concat();
console.log(numbersClone); // => [1, 2, 3, 4]
// concat doesn't change the original array
console.log(numbers); // => [1, 2, 3, 4]
Copy an array using Array.from, a utility function from javascript that performs a shallow copy of an array or array-like object.
let numbers = [1, 2, 3, 4];
let numbersClone = Array.from(numbers);
console.log(numbersClone); // => [1, 2, 3, 4]
The spread operator is three dots in the form of .... One of the ways to use the spread operator is to copy an item of an array and spread it into a new one.
let numbers = [1, 2, 3, 4];
// the ... spreads the elements of numbers array into a new array
let numbersClone = [...numbers];
console.log(numbersClone); // => [1, 2, 3, 4]
The spread operator is really powerful, it can also be used to:
In the example below, multiple arrays are cloned, and values are added in between.
let numbers1 = [1, 2, 3, 4];
let numbers2 = [100, 200];
// cloning values of numbers1
// adding 45 in the middle
// and cloning values of numbers2
let largeNumbers = [...numbers1, 45, ...numbers2];
// other arrays are copied and the order was maintained
console.log(largeNumbers); // => [1, 2, 3, 4, 45, 100, 200];
// original array is not effected
console.log(numbers1); // => [1, 2, 3, 4];
There are always many ways to do the same thing. Use the one that suits your needs, keeps things simple, and meets your team's standards!
thanks for reading
Subscribe and follow me Twitter to get more tips on all things software.