为什么散布元素不适合复制多维数组? [英] Why is a spread element unsuitable for copying multidimensional arrays?

查看:144
本文介绍了为什么散布元素不适合复制多维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自mdn:传播语法

注意:通常,ES2015中的散布运算符在复制阵列时会深入一层.因此,它们不适合复制多维数组.使用Object.assign()和对象传播语法是相同的情况.请看下面的示例,以更好地理解.

Note: Typically the spread operators in ES2015 goes one level deep while copying an array. Therefore, they are unsuitable for copying multidimensional arrays. It's the same case with Object.assign() and Object spread syntax. Look at the example below for a better understanding.

var a = [[1], [2], [3]];
var b = [...a];
b.shift().shift(); // 1
// Now array b is: [[2], [3]]

以上陈述的意义是什么?上面的代码示例的工作原理与使用.slice()方法将数组从a复制到b的工作原理相同.我尝试在此处向数组添加另一个维度: https://repl.it/HKOq/2 和一切仍然按预期进行.

What is the point of the above statement? The above code sample works just the same as if you'd copied the array in a to b using the .slice() method. I tried adding another dimension to the array here: https://repl.it/HKOq/2 and things still worked as expected.

那么为什么散布语法不适合复制多维数组?

So why is the spread syntax unsuitable for copying multidimensional arrays?

我将不胜感激.

阅读estus和vol7ron的答案有助于我弄清楚事情.基本上,正如estus指出的那样,数组内部只有数组,而不是多维数组.

Reading the answers by estus and vol7ron helped me figure things out. Basically, as estus points out technically there are just arrays inside arrays rather than multidimensional arrays.

正如vol7ron解释的那样,仅复制数组的第一级,因此内存中的对象对于任何其他嵌套元素都保持不变.

And as vol7ron explains only the first level of the array is copied so the objects in memory remain the same for any further nested elements.

我也错误地怀疑使用散布语法与切片运算符的行为应有所不同

I was also wrong to suspect that using the spread syntax was supposed to behave any differently than the slice operator

推荐答案

数组是对象,并且[...a]创建a数组对象的副本.

Arrays are objects, and [...a] creates a shallow copy of a array object.

对于语言本身,没有多维数组-数组中还有另一个数组.包含数组,纯对象,函数还是基元都没有关系.对于基元,将复制它们的值.否则,将复制对对象的引用.这就是

For the language itself there are no multidimentional arrays - there are another arrays inside an array. It doesn't matter if contains arrays, plain objects, functions or primitives. For primitives, their values will be copied. Otherwise, the references to objects will be copied. This is what

与Object.assign()和对象散布运算符相同的情况

It's the same case with Object.assign() and Object spread operators

部分是指.

关于

上面的代码示例的工作原理与使用.slice()方法将数组从a复制到b的工作原理相同

The above code sample works just the same as if you'd copied the array in a to b using the .slice() method

...确实如此.这是写a.slice()[].concat(a)的更整洁的方法.除相当大的例外. ES6 rest运算符(以及Array.from(a))同样适用于所有可迭代对象,而不仅仅是数组.

...it truly does. This is a neater way to write a.slice() or [].concat(a). With a considerable exception. ES6 rest operator (as well as Array.from(a)) works equally for all iterables, not just for arrays.

对于对象的副本,ES6没有提供任何新内容,应手动递归复制对象(数组).为了解决所有问题,使用经过验证的第三方帮助器功能(例如Lodash cloneDeep)仍然有意义.

For a deep copy of an object ES6 offers nothing new, an object (which an array is) should be recursively copied by hand. To address all the concerns it still makes sense to use proven third-party helper functions, such as Lodash cloneDeep.

这篇关于为什么散布元素不适合复制多维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆