Summary of JS array cloning methods

Source: Internet
Author: User

Therefore, if you want to create an object with the same content as an existing object, you cannot simply assign values. This may be hard to understand.

See the following example:CopyCodeThe Code is as follows: var A = [1, 2, 4]; var B = A; C = B;. pop (); // remove the last element alert (B); // pop up 1, 2, 3 alert (c); // pop up 1, 2, 3
VaR A = [1, 2, 4]; var B = A; C = B;. pop (); // remove the last element alert (B); // pop up 1, 2, 3 alert (c); // pop up 1, 2, 3

The code above shows that after the content of A is changed, the results of variables B and C are also changed.

The above situation may not be what we need. What we want is to create a "new" object with the same content as the original object. In this way, we need to use clone methods.

Array in JS is also an object. Here we mainly summarize the cloning method of array. Here we extend a clone method for the native object of array.

1. The simplest way is to create a new array and traverse the array to add it to the new array one by one.Copy codeThe Code is as follows: array. prototype. clone = function () {var A = []; for (VAR I = 0, L = This. length; I <L; I ++). push (this [I]); return ;}
Array. prototype. clone = function () {var A = []; for (VAR I = 0, L = This. length; I <L; I ++). push (this [I]); return ;}

This implementation method is the easiest to think of and the easiest to understand, but the code is a bit complicated. We carefully study some methods of array, but there is actually a very simple method. The other two methods are described below.

2. Use the Slice Method of the array object.

The Slice Method returns a segment of the array through the input values of the start and end parameters. This method does not operate on the original array. We can use slice (0) to return all items.Copy codeThe Code is as follows: array. Prototype. Clone = function () {return this. Slice (0 );}
Array. Prototype. Clone = function () {return this. Slice (0 );}

3. Use the Concat method of the array object.
The Concat method is used to merge arrays. By merging with an empty function, we can implement the clone function.Copy codeThe Code is as follows: array. prototype. clone = function () {return []. concat (this);} // or array. prototype. clone = function () {return this. concat ();}
Array. prototype. clone = function () {return []. concat (this);} // or array. prototype. clone = function () {return this. concat ();}

There should be other methods if you give full play to your imagination. The above are just two methods that I have come up.

The easiest way to clone arrays using JavaScript

The slice () function) and the sort, out-of-order, and search (sort () function) functions of JavaScript Arrays)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.