Detailed explanation of JavaScript Array replication and javascript Array

Source: Internet
Author: User
Tags javascript array

Detailed explanation of JavaScript Array replication and javascript Array

Previous

The previous blog post on copying objects describes object copying. This article describes how to copy arrays in detail.

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
The disadvantage of using this method is that all the items in the array have become strings.

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] 

Deep copy

The method above implements only the shortest copy of the array. If you want to implement the deep copy of the array, you need to use the recursive method.

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]

Related Article

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.