这是代码,在这里我将数组传递给此react函数,
const Lifeofgame = arrayarg=> {
const result = ([...arrayarg]);
const tempstatuslist = ([...arrayarg]);
console.log("the props values are inside log inside templist is");
console.log(tempstatuslist);
result[0][2]=true;
console.log(`result is ${result} tempstatuslist is ${tempstatuslist}`);
}
在这里,函数获取一个数组(矩阵)作为参数。然后使用传播运算符将该参数的值复制到两个diff变量中。在更新一个值时,另一个也在更新。
销毁二维数组不会将Array复制到新变量。它复制每个子数组的地址并将它们合并到一个新的数组中,因此,当您更改一个值时,它将更改原始数组。你可以这样解决
const result = JSON.parse(JSON.stringify(arrayarg));
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句