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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Using GroupBy on an array of objects in JavaScript

83 点作者 saranshk超过 3 年前

10 条评论

yashap超过 3 年前
Personally I’d have this return a map instead of an object, so that the keys aren’t forced to be strings. Otherwise seems like a reasonable API, very similar to groupBy in any other moderately functional language, except with the “keys must be strings” restriction inherited from returning a plain object.<p>Also, for those thinking JS objects can have non-string keys, they can’t, it just sometimes appears that way due to JS type coercion: <a href="https:&#x2F;&#x2F;www.becomebetterprogrammer.com&#x2F;can-javasscript-object-keys-be-numbers-or-non-string-values&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.becomebetterprogrammer.com&#x2F;can-javasscript-objec...</a><p>Edit: as pointed out by shawnz, this isn’t entirely accurate, JS object keys can also be symbols.
评论 #29726226 未加载
评论 #29727816 未加载
评论 #29729463 未加载
评论 #29726790 未加载
评论 #29726411 未加载
ksbrooksjr超过 3 年前
Unfortunately the typescript definitions for these two methods haven&#x27;t been written yet [1].<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;microsoft&#x2F;TypeScript&#x2F;issues&#x2F;47171" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;microsoft&#x2F;TypeScript&#x2F;issues&#x2F;47171</a>
jbverschoor超过 3 年前
Oh lord… so you group by a Number, but the map-key becomes a String. Sounds like idiomatic JavaScript
评论 #29726427 未加载
评论 #29727425 未加载
评论 #29726607 未加载
评论 #29726425 未加载
chris-orgmenta超过 3 年前
Great - lodash needed a little less now.<p>Though I do often see people using a combination of maps, filters, reduces, lodash functions etc. one by one, rather than doing everything in a single for in loop. In most cases it feels that For In is still the most performant option, as you want to manipulate the data in more ways than just grouping
zackmorris超过 3 年前
For what it&#x27;s worth, the Laravel PHP framework (no affiliation) has the best introduction to higher-order methods for imperative programmers used to Javascript&#x2F;C style of anything that I&#x27;ve come across:<p><a href="https:&#x2F;&#x2F;laravel.com&#x2F;docs&#x2F;master&#x2F;collections" rel="nofollow">https:&#x2F;&#x2F;laravel.com&#x2F;docs&#x2F;master&#x2F;collections</a><p>Most of these methods are also available as part of the Eloquent ORM, for filtering&#x2F;reshaping queries before they&#x27;re executed:<p><a href="https:&#x2F;&#x2F;laravel.com&#x2F;docs&#x2F;master&#x2F;eloquent" rel="nofollow">https:&#x2F;&#x2F;laravel.com&#x2F;docs&#x2F;master&#x2F;eloquent</a><p>Since being exposed to this way of working, I rarely use foreach() anymore, much less for(). The main downside being that I find most other languages tedious to work in now. LINQ in .NET&#x2F;C# is nice, there might be others.
badjeans超过 3 年前
Hardly shorter than just writing the for loop, i.e. the example in python<p><pre><code> groupByAge = collections.defaultdict(list) for person in people: groupByAge[person[&quot;age&quot;]].append(person)</code></pre>
评论 #29728472 未加载
newlisp超过 3 年前
I normally just write<p><pre><code> const groupBy = (fn, arr) =&gt; { return arr.reduce((o, e) =&gt; { const k = fn(e) if (o[k] !== undefined) o[k].push(e) else o[k] = [e] return o } Object.create(null)) }</code></pre>
评论 #29727402 未加载
duxup超过 3 年前
I was just doing something like this and thinking “I do this in SQL all the time but users like to change things up so much it should be a native array thing.”
评论 #29726851 未加载
mahesh_rm超过 3 年前
Where was this when I needed it? Happy discovery.
评论 #29726863 未加载
mberning超过 3 年前
I worked on a Ruby gem to add group_by and aggregate_by functions to collections&#x2F;enumerables. I finished about 1&#x2F;2 of the functionality and found the implementation to be hateful and thought it would be received poorly. Now seeing this I am second guessing myself.
评论 #29731002 未加载
评论 #29727447 未加载