For JavaScript, arrays are reference types. If you want to copy an array, you have to think about it, because functions including concat and slice are all copied in a shallow layer. In other words, for a two-dimensional array, using concat to copy, the second-dimensional array is still a reference. Modifying the new array will also change the old array.
So, I want to write a deep copy function to help with deep copying of groups.
Generally speaking, the assignment can be achieved using "=". However, for reference types such as arrays, objects, functions, etc., this symbol is not good.
1. Simple copying of arrays
1.1 Simple traversal
The simplest and most basic way is naturally circular processing. Example:
JavaScript
function array_copy(arr) { var out = [], i, len; if (out[i] instanceof Array === false){ return arr; } for (i = 0, len = ; i < len; i++) { if (out[i] instanceof Array){ out[i] = deepcopy(arr[i]); } else { out[i] = arr[i]; } }; return a; } //testvar arr1 = [1, 2, 3, 4], arr2 = array_copy(arr1); (arr1, arr2); arr2[2] = 10; (arr1[2], arr2[2]); function array_copy(arr) { var out = [], i, len; if (out[i] instanceof Array === false){ return arr; } for (i = 0, len = ; i < len; i++) { if (out[i] instanceof Array){ out[i] = deepcopy(arr[i]); } else { out[i] = arr[i]; } }; return a; } //testvar arr1 = [1, 2, 3, 4], arr2 = array_copy(arr1); (arr1, arr2); arr2[2] = 10; (arr1[2], arr2[2]);
1.2 Workaround copy implementation
The trick that often appears in interview questions is to use slice or contcat methods to implement it. Example:
JavaScript
var arr1 = [1, 2, 3, 4], arr2 = (0), arr3 = (); (arr1, arr2, arr3); arr2[2] = 10; arr3[2] = 11; (arr1[2], arr2[2], arr3[2]); var arr1 = [1, 2, 3, 4], arr2 = (0), arr3 = (); (arr1, arr2, arr3); arr2[2] = 10; arr3[2] = 11; (arr1[2], arr2[2], arr3[2]);
2. Deep copying of arrays
For ordinary one-dimensional arrays and values are non-referenced types, there is no problem using the above method, otherwise it will be more troublesome. Deep copying requires consideration of the case where the array value is of various reference types.
2.1 Using the JSON method
(array) and then (). Example:
JavaScript
var arr1 = [1, 2, [3, 4], {a: 5, b: 6}, 7], arr2 = ((arr1)); (arr1, arr2); arr2[1] = 10; arr2[3].a = 20; (arr1[1], arr2[1]); (arr1[3], arr2[3]); var arr1 = [1, 2, [3, 4], {a: 5, b: 6}, 7], arr2 = ((arr1)); (arr1, arr2); arr2[1] = 10; arr2[3].a = 20; (arr1[1], arr2[1]); (arr1[3], arr2[3]);
This method has compatibility issues with ancient browsers. If compatibility is indeed required, the following compatible files can be introduced to solve the problem:
/douglascrockford/JSON-js/blob/master/
Note: If the array value is a function, the above method still does not work.
2.2 Full implementation of deep replication
Considering the nesting of multi-dimensional arrays and the array value is an object, the following prototype extension can be made (of course, it is also possible to write it as an ordinary function implementation, and prototype extension is not recommended):
JavaScript
= function () { var o = {}; for (var i in this) { o[i] = this[i]; } return o; }; = function () { var arr = []; for (var i = 0; i < ; i++) if (typeof this[i] !== 'object') { (this[i]); } else { (this[i].clone()); } return arr; }; //Test 1 Objectvar obj1 = { name: 'Rattz', age: 20, hello: function () { return "I'm " + name; } }; var obj2 = (); ++; (); //Test 2 Arrayvar fun = function(log) {}, arr1 = [1, 2, [3, 4], {a: 5, b: 6}, fun], arr2 = (); (arr1, arr2); arr2[2][1]= 'Mike'; arr2[3].a = 50; arr2[4] = 10; (arr1, arr2); = function () { var o = {}; for (var i in this) { o[i] = this[i]; } return o; = function () { var arr = []; for (var i = 0; i < ; i++) if (typeof this[i] !== 'object') { (this[i]); } else { (this[i].clone()); } return arr; //Test 1 Objectvar obj1 = { name: 'Rattz', age: 20, hello: function () { return "I'm " + name; } var obj2 = (); (); //Test 2 Arrayvar fun = function(log) {}, arr1 = [1, 2, [3, 4], {a: 5, b: 6}, fun], arr2 = (); (arr1, arr2); arr2[2][1]= 'Mike'; arr2[3].a = 50; arr2[4] = 10; (arr1, arr2);
2.3 Use jQuery's extend method
If you are using jQuery, the easiest way is to use the extend plugin method. Example:
JavaScript
var arr1 = [1, 2, [3, 4], {a: 5, b: 6}, 7], arr2 = $.extend(true, [], arr1); (arr1, arr2); arr2[1] = 10; (arr1, arr2); var arr1 = [1, 2, [3, 4], {a: 5, b: 6}, 7], arr2 = $.extend(true, [], arr1); (arr1, arr2); arr2[1] = 10; (arr1, arr2);
The above is the in-depth copying and parsing of JavaScript arrays introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!