温馨提示:本文翻译自stackoverflow.com,查看原文请点击:ecmascript 6 - javascript es6 array feature [...data, 0] "spread operator"
ecmascript-6 javascript

ecmascript 6 - javascript es6数组功能[... data,0]“ spread operator”

发布于 2020-03-29 13:10:17

我在一些示例代码中遇到了这个问题,但我完全迷路了。

const addCounter = (list) => {
    return [...list, 0];  // This is the bit I am lost on, and I don't know about [...list, 0]
}

显然,以上内容等于以下内容:

const addCounter = (list) => {
    return list.concat([0]);
}

任何建议或解释将不胜感激。

查看更多

查看更多

提问者
Bill
被浏览
84
24.3k 2018-03-24 01:55

...list正在使用传播语法传播的元素list假设列表为[1, 2, 3]因此[...list, 0]变为:

[1, 2, 3, 0]

与执行结果相同 list.concat([0]);

这不是ES6中数组的功能,它仅用于数组串联。它还有其他用途。阅读有关MDN的更多信息,或查看此问题