Function objectify

Convert an array to a dictionary by mapping each item into a dictionary key & value

const fish = [
{
name: 'Marlin',
weight: 105
},
{
name: 'Bass',
weight: 8
},
{
name: 'Trout',
weight: 13
}
]
objectify(fish, f => f.name) // => { Marlin: [marlin object], Bass: [bass object], ... }
objectify(
fish,
f => f.name,
f => f.weight
) // => { Marlin: 105, Bass: 8, Trout: 13 }
  • Type Parameters

    • T
    • Key extends string | number | symbol
    • Value = T

    Parameters

    • array: readonly T[]
    • getKey: ((item: T) => Key)
        • (item): Key
        • Parameters

          • item: T

          Returns Key

    • getValue: ((item: T) => Value) = ...

    Returns Record<Key, Value>