What can I use as placeholders in the destruction of the es6 array?

I don't like it here , ,:

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

Can I use some placeholder characters? I would prefer not to introduce unused variables, I just want to make the code more understandable. Right now the only thing I can think of is comments:

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

Any better ideas?

+4
source share
1 answer

There is no placeholder concept in JS. Often used for this _, but you cannot use it more than once in a single ad:

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

Also, it _can actually be used in your code, so you have to come up with a different name, etc.

:

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

Source: https://habr.com/ru/post/1626545/


All Articles