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
- 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
- VaR arr = [1, 2, 4];
- VaR clone = arr. Slice (0 );
- Arr. splice (); // deletes two elements starting from position 1.
- 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
- VaR;
- 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 ));
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
- 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
- VaR arr = [1, 2, 4];
- VaR clone = arr. Concat ();
- Arr. splice (); // deletes two elements starting from position 1.
- 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
- VaR arr = [1, 2, 2, 3], 4];
- VaR clone = arr. Slice (0 );
- Arr [2]. splice (); // deletes two elements starting from position 1.
- 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
- Source: http://www.cnblogs.com/birdshome/archive/2005/03/20/122246.html
- Object. Prototype. Clone = function ()
- {
- VaR objclone;
- If (this. constructor = Object) objclone = new this. Constructor ();
- Else objclone = new this. Constructor (this. valueof ());
- For (var key in this)
- {
- If (objclone [Key]! = This [Key])
- {
- If (typeof (this [Key]) = 'object ')
- {
- Objclone [Key] = This [Key]. Clone ();
- }
- Else
- {
- Objclone [Key] = This [Key];
- }
- }
- }
- Objclone. tostring = This. tostring;
- Objclone. valueof = This. valueof;
- Return objclone;
- }