How to Clone an Array in JavaScript?2 Mar 2025 | 4 min read IntroductionJavaScript is one of the languages for web development. It can perform many activities. Also, there are many methods for sorting, scrolling, selecting, slicing, and finding to work on the entire array. Here in this article, we will check each method by performing and also we will learn how to duplicate an array. We need to create a new array that needs to be the same as the main array or the original array containing the same numbers to change an array in JavaScript. There are many methods to clone an array in JavaScript. We will check every method to clone an array using the below-provided methods: Array.slice()Array.slice is a method that will remove particular entries or numbers in the original array. We can use this method whenever we do not need some numbers. We need to first split the array and copy a set by choosing start and endpoints. The following code shows how to slice and Array. Code Output ![]() If the slice() method is invoked without an argument, it copies the original array from a particular index to the end. Sliced array is nothing but a duplicate of mainArray. Above is the situation where the slice() function returns "[2]" based on the numbers (1,2). Numbers range from 0 to 2. It is important to note that when we are using the slice() method with multiple arrays, references are assigned instead of values. Array.concat()This method will provide us a new array by adding or merging or concatenating many arrays. Let us discuss this concatenation method below: Code Output ![]() Array.mapThe Array.map() method will give us an output which is also an array that is same in size as the input array. Map() generates a fresh array for every item in an array. Map() function will not work if there is an empty inside. The map function does not alter the original array. Code Output ![]() This program creates a copy of an array using the Array.map method. The map() function executes a specified function on each element of an array, returning the results in a new array. Array.filterThe Array.filter() function, similar to map(), outputs an array, but the resulting array may not always be the same size. Filter() examines a function & a condition and gives back the element when the condition is satisfied. Code Output ![]() The program mentioned creates a copy of the array by utilizing the Array.filter method. The filter() function evaluates criteria within the nums list. If the requirement is fulfilled, the number will be placed in a new array called numsClone. Array.reduceArray.reduce() iterates through each element of an array and produces a single value or object representing the final outcome of the function. Syntax: Code Output ![]() The reduce function is employed to decrease the quantity of numbers in the program mentioned. In this instance, we begin with a normalArray that is empty, then we progressively include each element from the "num" array. The array must be returned for the method to be usable in the next iteration. The reference to the array numClone is given back. The number array is going to be duplicated in the numClone array. JSON.stringify and JSON.parseJSON.stringify turns an object into a string and JSON.parse turns a string into an object. When you combine these things, you can turn something into a string and then back into a new type of information. JSON.parse() and JSON.stringify() are used for multilevel deep copying. They copy values instead of references at any level and work with multi-dimensional arrays. Code Output ![]() The above program uses JSON.stringify to turn Array1 into a string. Using JSON.parse turns the string into a multi-dimensional array called Array2. This method returns a copy of the array's elements instead. The output shows that changing Array2 doesn't affect Array1. Note: You can safely copy deep-nested arrays with this method.Conclusion
|
JavaScript is a versatile programming language widely used for web development. It offers a range of features to handle data and iterate over collections efficiently. Two powerful concepts, iterators and generators, enhance the language's capabilities, enabling developers to work with data structures more flexibly and concisely....
6 min read
An event in ReactJS is any action or occurrence that the React application can detect and handle. React events are similar to traditional DOM events, but React handles some of the complexity and offers a standardized and effective method for handling and managing events in...
4 min read
We will learn the JavaScript formatter in this article. What is a formatter? Formatter is a tool that is utilized to reduce the whitespaces from the JavaScript code. It helps in reducing the size of the code which reduces the size of data and makes it easy...
4 min read
What is ? In JavaScript, assertions are statements that check whether a given condition is true. It is often used for debugging and testing purposes to ensure that certain conditions or assumptions about your code are valid. In simple words, with the use of assertions, we can include...
7 min read
A simple mechanism for manually triggering activities on DOM factors is dispatchEvent in JavaScript. It is a critical thing of the Document Object Model (DOM) API, which allows us programmers to simulate personal interactions to create dynamic, fairly interactive online programs. We need to observe a...
8 min read
String comparison is a foundational aspect of programming. In particular, web development frequently depends on user input, data validation, and conditional logic. It is where string comparison is often utilized. A trusted language, JavaScript has many methods to compare strings. This article delves into different techniques...
3 min read
What is JavaScript? JavaScript is a scripting language that is used both on the client side and server side that help the developer to make the web page attractive. In other words, JavaScript is a text-based method that allows the developer to create dynamically updating content, control...
4 min read
? 1. The Fundamentals of IIFE 1.1 Overview In JavaScript, a function referred to as an Immediately Invoked Function Expression (IIFE) is known immediately upon definition. An on-the-spot invocation or execution of a feature expression defines an IIFE, as counseled by its call. Regular features, alternatively, need to be...
9 min read
In software development, code formatting pertains to the visual presentation and organization of source code. It consists of aspects such as layout, spacing, line breaks, naming rules, and consistent use of syntax elements like braces and semicolons. Despite possibly seeming unimportant in comparison to functionality, code...
7 min read
A basic programming procedure is shuffling an array specifically where randomization is involved, as in games limitations or simply creating randomized lists. The Fisher-Yates shuffle, also known as the Knuth shuffle, can be used to shuffle a JavaScript array. The math.random() method is used to sort...
10 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India