@eroyeroy/utils - v0.1.0
    Preparing search index...

    Function mapArrayToRecord

    • Transforms an array into a record keyed by the value of key. Later items overwrite earlier items if they share the same key value. Items where the key value is not a valid property key are skipped.

      Type Parameters

      • T
      • K extends string | number | symbol

      Parameters

      • array: readonly T[]

        The array to transform.

      • key: K

        The key whose value becomes the record key.

      Returns Record<Extract<T[K], PropertyKeyValue>, T>

      mapArrayToRecord([{ id: 1, name: "Alice" }, { id: 2, name: "Bob" }], "id")
      // => { 1: { id: 1, name: "Alice" }, 2: { id: 2, name: "Bob" } }