TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Sparse array destructuring in JavaScript

13 pointsby suhairover 2 years ago

6 comments

bastawhizover 2 years ago
It&#x27;s called &quot;array destructuring&quot; because you&#x27;re destructuring _arrays_, but this is treating arrays like _tuples_. If you&#x27;re storing more than 3 (really 2) pieces of information about a single thing in a tuple, you&#x27;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]) =&gt; value % 2 === 0) .map(([key, value]) =&gt; `${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&#x27;s happening. But if you&#x27;re doing this for a tuple of any significant length, you&#x27;re just papering over a much more serious problem that&#x27;s already present in your code.
评论 #33635038 未加载
TAForObvReasonsover 2 years ago
Alternative:<p><pre><code> let { 0: one, 2: three, 4: five } = nums;</code></pre>
评论 #33635127 未加载
gregorsover 2 years ago
I&#x27;d at least place an underscore as is common in other languages<p>let [one, _, three, _, five] = nums<p>edit: I guess you can&#x27;t do that in JS, so<p>let [one, _two, three, _four, five] = nums
jakearover 2 years ago
I&#x27;ve used this for regexp&#x27;s...<p><pre><code> const parse = &#x2F;(\w+):(\d+)&#x2F;.exec(str) if (parse) { const [, key, value] = match } </code></pre> (Where the elided first entry is the original string)<p>I&#x27;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 = &#x2F;(?&lt;key&gt;\w+):(?&lt;value&gt;\d+)&#x2F;.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:&#x2F;&#x2F;github.com&#x2F;microsoft&#x2F;TypeScript&#x2F;issues&#x2F;32098" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;microsoft&#x2F;TypeScript&#x2F;issues&#x2F;32098</a> would help)
travisdover 2 years ago
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]).
评论 #33635115 未加载
评论 #33635045 未加载
friedman23over 2 years ago
I code typescript every day. I don&#x27;t really hate javascript. This is terrible.
评论 #33635384 未加载
评论 #33635170 未加载