my-swiss-knife: - v1.9.9
    Preparing search index...

    Function createHashmapFromArr

    • Accepts array of objects and return the hash map object, in which the value of provided refField will be used as a key

      Type Parameters

      Parameters

      • data: D[]

        The array of objects

      • refField: K

        The key of the object whose value will be used as the key in the hashmap

      Returns HashMapType<D, K>

      • Hashmap
      export const createHashmapFromArr = <
      D extends ObjectType,
      K extends keyof D = StringNumberKeys<D>,
      >(
      data: D[],
      refField: K,
      ): HashMapType<D, K> => {
      return data.reduce((acc, item) => {
      const refKey = item[refField]
      if (typeof refKey === 'string' || typeof refKey === 'number') {
      acc[refKey] = item
      }
      return acc
      }, {} as HashMapType<D, K>)
      }