I miss being able to create a hash from an array in a quick way. You always end up with
let bar = [{id: zz, }, {id: y},...]
let foo = {}
bar.forEach(v => foo[v.id] = v)
I'd love to have something like:
let foo = bar.toMap(v => [v.id, v])
let bar = [{id: zz, }, {id: y},...]
let foo = {}
bar.forEach(v => foo[v.id] = v)
I'd love to have something like:
let foo = bar.toMap(v => [v.id, v])
You still need to create the middle datastructure ( [[k1,v1], [k2, v2]] ) but it is an improvement :)
let foo = Object.fromEntries(bar.map((v) => [v.id, v])); import { map } from "my-iter-lib";
Object.fromEntries(map((v) => [v.id, v], bar));
Or with the proposed iterator-helpers: Object.fromEntries(
Iterator.from(bar).map((v) => [v.id, v]),
)
However the inner arrays are harder to get rid of... In fact even OP’s oneliner defines the inner arrays. I would hope there were some engine optimizations though which could minimize their footprint.