我可以在 es6 数组解构中使用什么作为占位符?

What can I use as placeholders in es6 array destructuring?

我不喜欢这里的 , ,:

let colors = [ "red", "green", "blue" ];
let [ , , thirdColor] = colors;

我可以使用一些占位符吗?我宁愿不引入未使用的变量,我只是想让代码看起来更清晰。现在我唯一能想到的就是评论:

let [/*first*/, /*second*/, thirdColor] = colors;

有更好的主意吗?

JS中没有占位符的概念。通常 _ 用于此目的,但实际上您不能在一个声明中多次使用它:

let [_, secondColor] = colors; // OK
let [_, _, thirdColor] = colors; // error

此外,_ 可能实际上在您的代码中使用,因此您必须想出另一个名称等。

最简单的方法可能是直接访问第三个元素:

let thirdColor = colors[2];
let {2: thirdColor, 10: eleventhColor} = colors;