Function mapToArray

Converts Map to Array with a provided transformer function. Useful for plucking out certain properties from contained values and for creating a new map based on transformed values from an input map.

let person = { age: 29, name: `John`};
map.add(person.name, person);

const ages = mapToArray(map, (key, person) => person.age);
// [29, ...]

In the above example, the transformer function returns a number, but it could just as well return a transformed version of the input:

// Return with random heights and uppercased name
mapToArray(map, (key, person) => ({
...person,
height: Math.random(),
name: person.name.toUpperCase();
}))
// Yields:
// [{height: 0.12, age: 29, name: "JOHN"}, ...]
  • Type Parameters

    • K
    • V
    • R

    Parameters

    • m: ReadonlyMap<K, V>
    • transformer: ((key: K, item: V) => R)

      A function that takes a key and item, returning a new item.

        • (key, item): R
        • Parameters

          • key: K
          • item: V

          Returns R

    Returns readonly R[]