How to use the JSON.parse() and JSON.stringify() Methods In Javascript

Using the JSON.parse() and JSON.stringify() methods is another way to clone an array in JavaScript. This method involves converting the original array to a JSON string and then parsing the JSON string to create a new array.

Example: This example shows the implementation of the above-explained approach.

Javascript
const originalArray = [1, 2, 3];
const clonedArray = JSON.parse(JSON.stringify(originalArray));
console.log(clonedArray);

Output
[ 1, 2, 3 ]

How to clone an array in JavaScript ?

In JavaScript, cloning an array means creating a new array with the same elements as the original array. Cloning an array in JavaScript is useful when you want to create a new array that has the same elements as an existing array, without modifying the original array.

Similar Reads

When to clone array?

If you want to perform some operations on an array, such as sorting, filtering, or mapping, but you don’t want to modify the original array, you can create a clone of the original array and perform the operations on the clone instead....

Using the Array.slice() Method

We use the slice method to create a shallow copy of an array. This method creates a new array with a subset of the elements from the original array....

Using the spread Operator

Using the spread operator … is a concise and easy way to clone an array in JavaScript. The spread operator allows you to expand an array into individual elements, which can then be used to create a new array....

Using the Array.from() Method

Using the Array.from() method is another way to clone an array in JavaScript. This method creates a new array from an existing array, using an optional mapping function to transform the values in the new array....

Using the Array.concat() Method

Using the Array.concat() method is another way to clone an array in JavaScript. This method creates a new array by concatenating two or more arrays together....

Using a for loop

This method involves iterating through each element in the original array and copying each element into a new array....

Using the Array.map() Method

Using the Array.map() method is another way to clone an array in JavaScript. This method creates a new array by mapping each element from the original array to a new value....

Using the Array.from() method with a map function

Using the Array.from() method with a map function is another way to clone an array in JavaScript. This method creates a new array by mapping each element from the original array to a new value using a provided function....

Using the Array.of() Method

This method creates a new array with the same elements as the original array....

Using the JSON.parse() and JSON.stringify() Methods

Using the JSON.parse() and JSON.stringify() methods is another way to clone an array in JavaScript. This method involves converting the original array to a JSON string and then parsing the JSON string to create a new array....

Using the Object.assign() Method

Using the Object.assign() method is another way to clone an array in JavaScript. This method creates a new array by copying the properties of the original array to a new object....

Using Array.reduce() Method

The Array.reduce() method executes a reducer function on each element of the array, resulting in a single output value. We can use it to construct a new array with the same elements as the original array....

Contact Us