It's called "array destructuring" because you're destructuring _arrays_, but this is treating arrays like _tuples_. If you're storing more than 3 (really 2) pieces of information about a single thing in a tuple, you're almost certainly misusing tuples. Why would you store a payment record, for instance, in a tuple of the form `[customerEmail, cardLast4, amount, currency, timestamp]` when you could just use an object, have consistent names that self-document the data structure, and never need to think about the ordering of elements in tuples?<p>This is really only even marginally appropriate if you want to do something like this:<p><pre><code> return Object.entries(myObject)
.filter(([, value]) => value % 2 === 0)
.map(([key, value]) => `${key} has even value ${value}`);
</code></pre>
Skipping the key in the `filter` call is probably straightforward enough that nobody is going to be confused about what's happening. But if you're doing this for a tuple of any significant length, you're just papering over a much more serious problem that's already present in your code.
I'd at least place an underscore as is common in other languages<p>let [one, _, three, _, five] = nums<p>edit: I guess you can't do that in JS, so<p>let [one, _two, three, _four, five] = nums
I've used this for regexp's...<p><pre><code> const parse = /(\w+):(\d+)/.exec(str)
if (parse) {
const [, key, value] = match
}
</code></pre>
(Where the elided first entry is the original string)<p>I've also had bugs where I forgot the leading comma and had all the wrong captures. Depending on the situation, I might instead recommend named captures:<p><pre><code> const parse = /(?<key>\w+):(?<value>\d+)/.exec(str)
if (parse) {
const {key, value} = parse.groups
}
</code></pre>
Caveat is it takes a bit to convince TS this is sound, and you have to manually sync the names. (<a href="https://github.com/microsoft/TypeScript/issues/32098" rel="nofollow">https://github.com/microsoft/TypeScript/issues/32098</a> would help)
I’d flag this if I saw this in a code review. It’s to cute and hard to see what’s happening. Would much rather just see someone prefix with an underscore if they don’t need the variable (const [id, _email, name] = loadUserInfo(…); is preferable to const [id, , name]).