简体   繁体   English

为什么拼接从所有变量中删除数组中的数据?

[英]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.我目前正在学习 javascript,我创建了一个数组,然后在 2 个变量 arrayBase 和 arrayData 中定义它,但是当我使用 splice 时,其他变量中的所有数据也被删除。 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?谁能帮助解释为什么 splice 使其他变量数组被删除? Thanks before!先谢过!

In Javasxript Objects (even arrays are object) are mutable means it can be modified .在 Javasxript 中,对象(甚至数组也是对象)是可变的,这意味着它可以被修改。

when you assign the objects (even array) to the new variable , the new variables point to the same objects.当您将对象(偶数数组)分配给新变量时,新变量指向相同的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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