简体   繁体   中英

why splice remove a data in array from all variable?

I am currently learning javascript, i create an array and then define it in 2 variable arrayBase and arrayData but when i use splice, all the data in other variable also get removed. I don't know what happen

 const arr=[1,2,3,4,5] const arrayBase=arr let max=0 let min=0 let arrayData=arr for(let i = 0; i<arrayBase.length;i++){ console.log(i,arrayBase) let x arrayData.splice(i,1) arrayData.forEach((item)=>x=x+item) if(x>max){ max=x } if(min<x){ min=x } arrayDataabc=arr }

Can anyone help explain why splice make other variable array get removed? Thanks before!

In Javasxript Objects (even arrays are object) are mutable means it can be modified .

when you assign the objects (even array) to the new variable , the new variables point to the same objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM