Effect CommunityEC
Effect Community3y ago
13 replies
Frederic

Transforming Array of Objects into Record of Objects

for the smart fp afficionados. If I want to transform an array of objects into a record of objects with the key being the value of some property of the object, is there a more idiomatic, or smarter approach than the following function ? (this was some fp-ts code)
const arrayToRecord = <A>(keyGetter: (i: A) => string) => (items: Array<A>): Record<string, A> =>
  pipe(
    items,
    A.reduce({}, (acc, item) => pipe(acc, REC.upsertAt(keyGetter(item), item)))
  )
Was this page helpful?