Hex Core JS
    Preparing search index...

    Function makeObjectFromArrayMap

    • Returns an object using the entries resulting from applying the given map function to the given array.

      This function optimizes the use of makeObjectFromEntries in conjuction with Array.map so that only one loop is needed instead of two (half as many iterations). In other words, is equivalent to makeObjectFromEntries(array.map(map)).

      Type Parameters

      • T
      • K extends string | number | symbol
      • V

      Parameters

      • array: T[]

        The array from which to derive entries (key-value pairs).

      • map: (e: T) => [K, V]

        The function to generate entries from array.

      Returns Record<K, V>

      const items = [{id: 1, name: "First"}, {id: 2, name: "Second"}, {id: 3, name: "Third"}];
      const itemLookup = makeObjectFromArrayMap(items, e => [e.id, e]);
      // result: {1: {id: 1, name: "First"}, 2: {id: 2, name: "Second"}, 3: {id: 3, name: "Third"}}

      const itemId = 3;
      const item = itemLookup[itemId];