由于數(shù)組不是js的基本類型,我們在對(duì)數(shù)組進(jìn)行備份嘴办,如果只是簡單的將它賦值給其他變量,那么我們只要改變其中任何一個(gè)买鸽,其他的變量的數(shù)組也會(huì)跟著改變涧郊。實(shí)際應(yīng)用中就會(huì)產(chǎn)生bug
// 淺復(fù)制
var arr = ["one","two","three"];
var arrto = arr;
console.log(arr, arrto); // ["one","two","three"] ["one","two","three"]
arrto[0] = 1;
console.log(arr, arrto); // [1,"two","three"] ["one","two","three"]
改變數(shù)組arrto,arr也會(huì)受到影響眼五,像這種直接賦值數(shù)組的方式就是淺復(fù)制妆艘,引發(fā)這種現(xiàn)象的原因是因?yàn)閿?shù)組是引用數(shù)據(jù)類型,arrto = arr 是將arr數(shù)組的引用地址傳遞給了 arrto看幼,arrto 與 arr 指向的是同一個(gè)數(shù)組批旺。要避免這類情況發(fā)生我們可以 對(duì)數(shù)組進(jìn)行深復(fù)制
// 深復(fù)制
var arr = ["one","two","three"];
var arrto = [];
for (var i = 0; i < arr.length; i++) {
arrto[i] = arr[i];
}
console.log(arr, arrto); // ["one","two","three"] ["one","two","three"]
arrto[0] = 1;
console.log(arr, arrto); // ["one","two","three"] [1,"two","three"]
arr數(shù)組中的每個(gè)元素都是字符串,屬于js的基本類型诵姜,可以直接傳遞值汽煮,遍歷數(shù)組arr將數(shù)組中的每個(gè)元素傳給新數(shù)組arrto就能實(shí)現(xiàn)數(shù)組的深復(fù)制。
使用一些js的函數(shù)也能實(shí)現(xiàn)數(shù)組的深復(fù)制
使用js內(nèi)置函數(shù)實(shí)現(xiàn)深復(fù)制
1.slice 可以從已有的數(shù)組中返回選定的元素棚唆,且返回的是一個(gè)新數(shù)組
var arr = ["one","two","three"];
var arrto = arr.slice(0);
console.log(arr, arrto); // ["one","two","three"] ["one","two","three"]
arrto[0] = 1;
console.log(arr, arrto); // ["one","two","three"] [1,"two","three"]
使用slice返回新數(shù)組后改變arrto的值暇赤,arr并未改變
2.concat : 連接兩個(gè)或多個(gè)數(shù)組,返回新數(shù)組
var arr = ["one","two","three"];
var arrto = arr.concat([]);
console.log(arr, arrto); // ["one","two","three"] ["one","two","three"]
arrto[0] = 1;
console.log(arr, arrto); // ["one","two","three"] [1,"two","three"]
3.map :返回一個(gè)由原數(shù)組中的每個(gè)元素調(diào)用一個(gè)指定方法后的返回值組成的新數(shù)組。
var arr = ["one","two","three"];
var arrto = arr.map(ele=>ele);
console.log(arr, arrto); // ["one","two","three"] ["one","two","three"]
arrto[0] = 1;
console.log(arr, arrto); // ["one","two","three"] [1,"two","three"]
總結(jié):數(shù)組由于不是js的基本類型宵凌,不能進(jìn)行直接傳值鞋囊,需要遍歷數(shù)組的每一個(gè)基本類型的元素傳值,最后構(gòu)建新的數(shù)組瞎惫。與數(shù)組類似 溜腐,js的對(duì)象也直接復(fù)制也會(huì)產(chǎn)生與數(shù)組相同的問題,要實(shí)現(xiàn)深拷貝瓜喇,需遍歷對(duì)象的每個(gè)屬性挺益。