by saranshk on 12/29/21, 3:39 PM with 31 comments
by yashap on 12/29/21, 4:16 PM
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: https://www.becomebetterprogrammer.com/can-javasscript-objec...
Edit: as pointed out by shawnz, this isn’t entirely accurate, JS object keys can also be symbols.
by ksbrooksjr on 12/29/21, 5:00 PM
by jbverschoor on 12/29/21, 4:20 PM
by chris-orgmenta on 12/29/21, 11:44 PM
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
by zackmorris on 12/29/21, 8:53 PM
https://laravel.com/docs/master/collections
Most of these methods are also available as part of the Eloquent ORM, for filtering/reshaping queries before they're executed:
https://laravel.com/docs/master/eloquent
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/C# is nice, there might be others.
by badjeans on 12/29/21, 6:12 PM
groupByAge = collections.defaultdict(list)
for person in people:
groupByAge[person["age"]].append(person)
by newlisp on 12/29/21, 5:46 PM
const groupBy = (fn, arr) => {
return arr.reduce((o, e) => {
const k = fn(e)
if (o[k] !== undefined) o[k].push(e)
else o[k] = [e]
return o
} Object.create(null))
}
by duxup on 12/29/21, 4:13 PM
by mahesh_rm on 12/29/21, 5:07 PM
by mberning on 12/29/21, 5:42 PM