Various ways of realizing deep copy of multidimensional array in JS

Source: Internet
Author: User
Tags array arrays copy implement reference return

Because JavaScript is divided into primitive types and reference types (similar to Java, C #). Array is a reference type, so directly with the = number assignment, only the address of the source array (or pointer) assigned to the destination array, and does not implement the data copy of the array. In addition to the one-dimensional array and multidimensional data deep copy implementation is not the same, discussed separately below.



First, error implementation

var array1 = new Array ("1", "2", "3");
var array2;
Array2 = array1;
array1.length = 0;
alert (array2); return to NULL

This is wrong because JavaScript is divided into primitive types and reference types (similar to Java, C #). Array is a reference type. Array2 gets a reference, so the modification of array1 affects array2.



How to implement a two or one-D array:

Use Slice () or concat () to implement a deep copy of a one-dimensional array, but this method applies only to one-dimensional arrays and is not valid for multidimensional arrays.

You can use slice () for replication because the slice () return is also an array.

var array1 = new Array ("1", "2", "3");
var array2;
Array2 = Array1.slice (0);
array1.length = 0;
alert (array2); Return 1, 2, 3



Note that concat () does not return a call to the function's array, but rather a new array, so you can use this to replicate.

var array1 = new Array ("1", "2", "3");
var array2;
Array2 = Array1.concat ();
array1.length = 0;
alert (array2); Return 1, 2, 3



Three, multidimensional array of JS function to achieve the way:
Copy Code

function Deepcopy (obj) {
var out = [],i = 0,len = Obj.length;
for (; i < Len; i++) {
if (Obj[i] instanceof Array) {
Out[i] = deepcopy (Obj[i]);
}
else out[i] = obj[i];
}
return out;
}

Here is the test code
var weekarray = new Array (7);//Array First dimension
var timetablearray = new Array (); Array Second dimension
var linearray = new Array (4); Array Third Dimension

Linearray[0] = "1_a";
LINEARRAY[1] = "1_b";
LINEARRAY[2] = "1_c";
Timetablearray.push (Linearray);
Weekarray[0] = deepcopy (Timetablearray);


Empty and add data for other weeks
Linearray.splice (0,linearray.length);
Timetablearray.splice (0,timetablearray.length);

Linearray[0] = "7_a";
LINEARRAY[1] = "7_b";
LINEARRAY[3] = "7_d";
Timetablearray.push (Linearray);
WEEKARRAY[7] = deepcopy (Timetablearray);

Alert ("weekarray=" + weekarray[0]);/return "1_a,1_b,1_c,"
Alert ("weekarray=" + weekarray[7]);//Return to "7_a,7_b,,7_d"



Four, multidimensional array of jquery implementation method:

Replace the Deepcopy function with the following function

Weekarray[0] = $.extend (true, {}, Timetablearray);



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.