admin管理员组文章数量:1188413
I came across this in some example code and I am completely lost.
const addCounter = (list) => {
return [...list, 0]; // This is the bit I am lost on, and I don't know about [...list, 0]
}
Apparently the above is equal to the following:
const addCounter = (list) => {
return list.concat([0]);
}
Any suggestion or explaination is much appreciated.
I came across this in some example code and I am completely lost.
const addCounter = (list) => {
return [...list, 0]; // This is the bit I am lost on, and I don't know about [...list, 0]
}
Apparently the above is equal to the following:
const addCounter = (list) => {
return list.concat([0]);
}
Any suggestion or explaination is much appreciated.
Share Improve this question edited Jun 14, 2018 at 20:44 goWithSwagger 1,7721 gold badge19 silver badges20 bronze badges asked Nov 24, 2015 at 9:59 BillBill 19.2k21 gold badges88 silver badges135 bronze badges 1- Does this answer your question? Spread Syntax vs Rest Parameter in ES2015 / ES6 – Henke - Нава́льный П с м Commented Mar 15, 2021 at 14:55
2 Answers
Reset to default 28...list
is using the spread syntax 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.
...list
spread
s (lays) out all the elements in the array list
.
so [...list, 0]
is all of the elements of list with a 0 at the end
本文标签: ecmascript 6javascript es6 array feature data0 quotspread operatorquotStack Overflow
版权声明:本文标题:ecmascript 6 - javascript es6 array feature [...data, 0] "spread operator" - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738386703a2084220.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论