javascript es6数组特征[... data,0]“扩展运算符” [英] javascript es6 array feature [...data, 0] "spread operator"
问题描述
const addCounter =(list )=> {
return [... list,0]; //这是我现在失去的一点[...列表,0]
}
显然以上是相同的。
const addCounter =(list)=> {
return list.concat([0]);
}
任何建议或解释都非常感激。
...列表
正在使用 spread operator 来传播元素 list
。我们假设列表是 [1,2,3]
。因此 [... list,0]
变成:
[1 ,2,3,0]
与列表相同的结果.concat([0]);
这不是ES6中数组的一个特性,它只是用于数组连接。它有其他用途。阅读更多 MDN ,或参见< a href =https://stackoverflow.com/questions/20541339/usage-of-rest-parameter-and-spread-operator-in-javascript>此问题。
I am come up with this in one of the example code and i am completely lost.
const addCounter = (list) => {
return [...list, 0]; // This is the bit i am lost i now about [...list, 0]
}
Apparently the above is equal the following.
const addCounter = (list) => {
return list.concat([0]);
}
Any suggestion or explain is much appreciated.
...list
is using the spread operator to spread the elements of list
. Let's assume the list is [1, 2, 3]
. Therefore [...list, 0]
becomes:
[1, 2, 3, 0]
Which has the same result as doing list.concat([0]);
This is not a feature of the array in ES6, it's just been used for array concatenation. It has other uses. Read more on MDN, or see this question.
这篇关于javascript es6数组特征[... data,0]“扩展运算符”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!