Js怎样实现对数组拷贝而不影响原变量
原创在JS基本类型可以直接分配, 更改新值后 不影响原始值
但如果是 数组操作 就比如:
let arr = [1,2,3,4];
let arr1 = [];
arr1 = arr;
arr1.push(5);
console.log(arr + "---" + arr1);
输出将发现其值已更改。, 变为 [1, 2, 3, 4, 5];
所以我们将用其他手术来弥补这一点, 不能允许他更改原始值。 因此,我们可以:
arr1 = arr.slice(0);
如果阵列包含对象, 或对象的深度副本 可以参考lodash库的deepClone方法
版权声明
所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除
相关文章
最新文章
- 0.05ms 往返:localhost 为什么不能直接当网站? 5小时前
- Fornax's Guide to Fast Ethernet 5小时前
- Possibly leaked NSA Cyberattack tool behind attacks (2017) 6小时前
- Steve Wozniak on Writing Apple Integer Basic 6小时前
- PayByPhone is stealing $50M/year from their users 6小时前
- Grand Theft Auto 6: A Landmark Achievement That Redefines a Console Generation 6小时前
- Research Acceleration: The Jagged Frontier 7小时前
- Google Embraces Geothermal Technology to Vaporize Rock for Clean Power 8小时前
- Microsoft's Agent Framework PyPI package compromised to steal credentials 9小时前
- Nvidia's Seven Billionaires Have Lost a Third of Their Wealth 9小时前
itfan123

