Skip to content

objectify

Convert a list to a dictionary object

97 bytes
since v12.1.0

Usage

Given an array of items, create a dictionary with keys and values mapped by given functions.

Parameters

  1. array: The input array to be converted into an object.
  2. getKey: A function that determines the key for each item in the array. It receives two arguments:
    • item: The current array item.
    • index: The index of the current item in the array.
  3. getValue (optional): A function that determines the value for each item in the array. If not provided, the original array item is used as the value. It also receives two arguments:
    • item: The current array item.
    • index: The index of the current item in the array.

Return Value

Returns a new dictionary object where keys and values are derived from the input array using the provided mapping functions.

Example

import * as _ from 'radashi'
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 }