Copying arrays in Javascript

Source: Internet
Author: User
Link: http://dinguangx.javaeye.com/blog/656130,http://qinglangee.javaeye.com/blog/160888
Copy an array:
The operation object in Javascript adopts the reference method like that in Java. Therefore, if you use the obj2 = obj1 method to copy arrays, changes made to both obj2 and obj1 affect the values of the two variables at the same time, leading to undesirable consequences. The following describes two methods for copying simple array elements.
(1) Copying Arrays Using slice ()
The slice () function returns the selected element from an existing array. Syntax: JS Code
  1. Arrobject. Slice (START, end); // start, start position; end, end position
Arrobject. Slice (START, end); // start, start position; end, end position

The slice () function returns a new array object, so you can use slice (0) to copy the array.
Test code: JS Code

  1. VaR arr = [1, 2, 4];
  2. VaR clone = arr. Slice (0 );
  3. Arr. splice (); // deletes two elements starting from position 1.
  4. Alert ("arr:" + arr + "\ nclone:" + clone); // arr: [1, 4] clone: [1, 2, 4]
VaR arr = [1, 2, 4]; var clone = arr. slice (0); arr. splice (); // Delete the two elements alert ("arr:" + arr + "\ nclone:" + clone) starting from position 1; // arr: [1, 4] clone: [1, 2, 3, 4]

JavaScript code

  1. VaR;
  2. A = new array ("A", "B", "C", "D", "E ");
  3. Document. Write (A. Slice (0, 3) + "<br> ");
  4. Document. Write (A. Slice (-3, 4) + "<br> ");
  5. Document. Write (A. Slice (0,-1) + "<br> ");
  6. Document. Write (A. Slice (1) + "<br> ");
  7. Document. Write (A. Slice (4, 3 ));
var a;a = new Array("a","b","c","d","e");document.write(a.slice(0,3) + "<br>");document.write(a.slice(-3,4) + "<br>");document.write(a.slice(0,-1) + "<br>");document.write(a.slice(1) + "<br>");document.write(a.slice(4,3));

This example uses the slice (START, [end]) method of the array object. This method returns a new array containing the elements specified by the source function from start to end, but does not include the end element, such as. slice (); If start is negative, it is processed as Length + start. Here, length is the length of the array, such as. slice (-3, 4), equivalent to. slice (2, 4 ). If end is negative, it is processed as Length + end. Here, length is the length of the array, for example, A. Slice (0,-1 ). If end is omitted, the slice method will always be copied to the end of the source array, for example, A. Slice (1 ). If end appears before start, no elements are copied to the new array, such as A. Slice ).

(2) Copying Arrays Using the Concat () function
Concat () is used to merge arrays. Syntax: JS Code

  1. Arrayobject. Concat (arrayx, arrayx,..., arrayx)
arrayObject.concat(arrayX,arrayX,......,arrayX)

Concat () is used for merging multiple arrays, but the returned result is a new array, instead of referencing any array used for merging. You can use this feature to connect an empty array with an array or directly clone the array without passing parameters.
Test code: JS Code

  1. VaR arr = [1, 2, 4];
  2. VaR clone = arr. Concat ();
  3. Arr. splice (); // deletes two elements starting from position 1.
  4. Alert ("arr:" + arr + "\ nclone:" + clone); // arr: [1, 4] clone: [1, 2, 4]
VaR arr = [1, 2, 4]; var clone = arr. concat (); arr. splice (); // Delete the two elements alert ("arr:" + arr + "\ nclone:" + clone) starting from position 1; // arr: [1, 4] clone: [1, 2, 3, 4]

However, the above two types of array replication operations are not suitable for arrays that contain complex data types. If an array contains complex data types, modification to the data still affects both the copied array and the copied array. Therefore, it is not a thorough solution to achieve deep replication.
Test code: JS Code

  1. VaR arr = [1, 2, 2, 3], 4];
  2. VaR clone = arr. Slice (0 );
  3. Arr [2]. splice (); // deletes two elements starting from position 1.
  4. Alert ("arr:" + arr + "\ nclone:" + clone); // arr: [1, 2, 3, 4] clone: [1, 2, 3, 4]
VaR arr = [1, 2, 2, 3], 4]; var clone = arr. slice (0); arr [2]. splice (); // Delete the two elements alert ("arr:" + arr + "\ nclone:" + clone) starting from position 1; // arr, 1, 3, 4] clone: [1, 2, 3, 4]

If you want to implement deep object replication, you can use the following generic function to implement object cloning: JS Code

  1. Source: http://www.cnblogs.com/birdshome/archive/2005/03/20/122246.html
  2. Object. Prototype. Clone = function ()
  3. {
  4. VaR objclone;
  5. If (this. constructor = Object) objclone = new this. Constructor ();
  6. Else objclone = new this. Constructor (this. valueof ());
  7. For (var key in this)
  8. {
  9. If (objclone [Key]! = This [Key])
  10. {
  11. If (typeof (this [Key]) = 'object ')
  12. {
  13. Objclone [Key] = This [Key]. Clone ();
  14. }
  15. Else
  16. {
  17. Objclone [Key] = This [Key];
  18. }
  19. }
  20. }
  21. Objclone. tostring = This. tostring;
  22. Objclone. valueof = This. valueof;
  23. Return objclone;
  24. }

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.