JavaScript數(shù)組復(fù)制詳解
前面的話
前面的博文中介紹了對象拷貝,本文將詳細(xì)介紹數(shù)組復(fù)制
push
function copyArray(arr){
var result = [];
for(var i = 0; i < arr.length; i++){
result.push(arr[i]);
}
return result;
}
var obj1=[1,2,3];
var obj2=copyArray(obj1);
console.log(obj1); //[1,2,3]
console.log(obj2); //[1,2,3]
obj2.push(4);
console.log(obj1); //[1,2,3]
console.log(obj2); //[1,2,3,4]
join
使用該方法的缺點(diǎn)是數(shù)組中的項(xiàng)全部變成了字符串形式
function copyArray(arr){
var result = [];
result = arr.join().split(',');
return result;
}
var obj1=[1,2,3];
var obj2=copyArray(obj1);
console.log(obj1); //[1,2,3]
console.log(obj2); //['1','2','3']
obj2.push(4);
console.log(obj1); //[1,2,3]
console.log(obj2); //['1','2','3',4]
concat
function copyArray(arr){
var result = [];
result = arr.concat();
return result;
}
var obj1=[1,2,3];
var obj2=copyArray(obj1);
console.log(obj1); //[1,2,3]
console.log(obj2); //[1,2,3]
obj2.push(4);
console.log(obj1); //[1,2,3]
console.log(obj2); //[1,2,3,4]
slice
function copyArray(arr){
var result = [];
result = arr.slice();
return result;
}
var obj1=[1,2,3];
var obj2=copyArray(obj1);
console.log(obj1); //[1,2,3]
console.log(obj2); //[1,2,3]
obj2.push(4);
console.log(obj1); //[1,2,3]
console.log(obj2); //[1,2,3,4]
深拷貝
以上方法實(shí)現(xiàn)的僅是數(shù)組的淺拷貝,如果要實(shí)現(xiàn)數(shù)組的深拷貝,需要使用遞歸方法
function copyArray(arr,result){
var result = result || [];
for(var i = 0; i < arr.length; i++){
if(arr[i] instanceof Array){
result[i] = [];
copyArray(arr[i],result[i]);
}else{
result[i] = arr[i];
}
}
return result;
}
var obj1=[1,2,[3,4]];
var obj2=copyArray(obj1);
console.log(obj1[2]); //[3,4]
console.log(obj2[2]); //[3,4]
obj2[2].push(5);
console.log(obj1[2]); //[3,4]
console.log(obj2[2]); //[3,4,5]
- jQuery中json對象的復(fù)制方式介紹(數(shù)組及對象)
- JavaScript 數(shù)組的深度復(fù)制解析
- javascript 三種數(shù)組復(fù)制方法的性能對比
- Javascript 復(fù)制數(shù)組實(shí)現(xiàn)代碼
- javascript復(fù)制對象使用說明
- js中如何復(fù)制一個(gè)對象并獲取其所有屬性和屬性對應(yīng)的值
- 原生js實(shí)現(xiàn)復(fù)制對象、擴(kuò)展對象 類似jquery中的extend()方法
- 深入理解JavaScript中的對象復(fù)制(Object Clone)
- 改進(jìn)版通過Json對象實(shí)現(xiàn)深復(fù)制的方法
- JavaScript數(shù)組和對象的復(fù)制
相關(guān)文章
uniapp在開發(fā)app時(shí)上傳文件時(shí)的問題記錄
在開發(fā)uniapp應(yīng)用時(shí),可能會遇到文件上傳功能在iOS和部分Android手機(jī)上不兼容的問題,經(jīng)過對比分析,發(fā)現(xiàn)問題可能出在文件的路徑上,通過使用uni.saveFile方法保存文件后,再上傳可以解決問題,這篇文章詳細(xì)介紹了解決方案,并引導(dǎo)讀者參考更多相關(guān)內(nèi)容2024-09-09
BootStrap 智能表單實(shí)戰(zhàn)系列(二)BootStrap支持的類型簡介
這篇文章主要介紹了BootStrap 智能表單實(shí)戰(zhàn)系列(二)BootStrap支持的類型簡介 的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧2016-06-06
typescript+react實(shí)現(xiàn)移動(dòng)端和PC端簡單拖拽效果
這篇文章主要為大家詳細(xì)介紹了typescript+react實(shí)現(xiàn)移動(dòng)端和PC端簡單拖拽效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
JavaScript 抽獎(jiǎng)效果實(shí)現(xiàn)代碼 數(shù)字跳動(dòng)版
直接上代碼吧,效果可以復(fù)制了去看2009-11-11
javascript面向?qū)ο髣?chuàng)建對象的方式小結(jié)
這篇文章主要介紹了javascript面向?qū)ο髣?chuàng)建對象的方式,結(jié)合實(shí)例形式總結(jié)分析了javascript常見的7種創(chuàng)建對象的方式,需要的朋友可以參考下2019-07-07
echarts圖形x、y坐標(biāo)文字設(shè)置間隔顯示及相關(guān)問題詳解
最近在做一個(gè)web的數(shù)據(jù)統(tǒng)計(jì)部分用到了Echart,下面這篇文章主要給大家介紹了關(guān)于echarts圖形x、y坐標(biāo)文字設(shè)置間隔顯示及相關(guān)問題的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08

