javascript es6 数组特性 [...data, 0] “扩展运算符"; [英] javascript es6 array feature [...data, 0] "spread operator"

查看:21
本文介绍了javascript es6 数组特性 [...data, 0] “扩展运算符";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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]
}

显然以上等价于以下:

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

非常感谢任何建议或解释.

Any suggestion or explaination is much appreciated.

推荐答案

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

...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]

与执行 list.concat([0]);

这不是 ES6 中数组的特性,它只是用于数组连接.它还有其他用途.阅读更多关于 MDN,或查看 这个问题.

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屋!

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