TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Sparse array destructuring in JavaScript

13 点作者 suhair超过 2 年前

6 条评论

bastawhiz超过 2 年前
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 未加载
TAForObvReasons超过 2 年前
Alternative:<p><pre><code> let { 0: one, 2: three, 4: five } = nums;</code></pre>
评论 #33635127 未加载
gregors超过 2 年前
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
jakear超过 2 年前
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)
travisd超过 2 年前
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 未加载
friedman23超过 2 年前
I code typescript every day. I don&#x27;t really hate javascript. This is terrible.
评论 #33635384 未加载
评论 #33635170 未加载