What can I use as placeholders in es6 array destructuring?

mik01aj

I don't like the , , here:

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

Can I use some placeholders characters? I'd rather not introduce unused variables, I just want to make the code look clearer. Right now the only thing I can think about are comments:

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

Any better ideas?

Felix Kling

There is no concept of a placeholder in JS. Often _ is used for this, but you can't actually use it more than once in one declaration:

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

Also, _ may actually be used in your code, so you'd have to come up with another name, etc.

The simplest way may be to access the third element directly:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

ES6 array destructuring for specific index

ES6 Array destructuring weirdness

Conditional destructuring array of objects es6

Destructuring and reordering Array with ES6 and React

Destructuring array of objects in es6

Destructuring an array of objects (es6)

What does this array-style destructuring on a function do in ES6?

What's the advantages of angular's dependency injection when I can use import/export in es6

How to use array destructuring and assign to an object property in JavaScript ES6

Can I use placeholders in an @Value annotated Resource?

Can I use destructuring somehow inside an if statement?

Can I use Destructuring to create a deep copy?

How can I use composition with methods in ES6 Classes?

Can I use loops to minimize ES6 import statements?

How can I use colors in ES6 template strings?

How can I use an es6 import in node?

Can I use Template String on object key in ES6?

What does the es6 { [a]: b } destructuring mean?

Destructuring to get the last element of an array in es6

ES6 nested array destructuring soft fail

ES6 destructuring to get the nth item from inner array

How to use destructuring assignment to define enumerations in ES6?

javascript es6: use case for destructuring rest parameter

How to use deep destructuring on imports in ES6 syntax?

JS: Use array destructuring (eslintprefer-destructuring)

How can I ignore certain returned values from array destructuring?

How can I get last element of an array by using Destructuring

How can I add array values to an object using destructuring?

Can i pass a array to constructor of an class by destructuring in javascript