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.

Using GroupBy on an array of objects in JavaScript

83 pointsby saranshkover 3 years ago

10 comments

yashapover 3 years ago
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 未加载
ksbrooksjrover 3 years ago
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>
jbverschoorover 3 years ago
Oh lord… so you group by a Number, but the map-key becomes a String. Sounds like idiomatic JavaScript
评论 #29726427 未加载
评论 #29727425 未加载
评论 #29726607 未加载
评论 #29726425 未加载
chris-orgmentaover 3 years ago
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
zackmorrisover 3 years ago
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.
badjeansover 3 years ago
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 未加载
newlispover 3 years ago
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 未加载
duxupover 3 years ago
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_rmover 3 years ago
Where was this when I needed it? Happy discovery.
评论 #29726863 未加载
mberningover 3 years ago
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 未加载