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

    Function updateArrayItemByKey

    • Returns a new array where every item matching item[key] === value is updated. update can be a partial object (merged with the item) or a function that returns a new item.

      Type Parameters

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

      Parameters

      • array: readonly T[]

        The source array.

      • key: K

        The key to match on.

      • value: T[K]

        The value to match.

      • update: Partial<T> | ((item: T) => T)

        Partial object or updater function.

      Returns T[]

      updateArrayItemByKey([{ id: 1, x: 0 }, { id: 2, x: 0 }], "id", 1, { x: 99 })
      // => [{ id: 1, x: 99 }, { id: 2, x: 0 }]

      updateArrayItemByKey([{ id: 1, x: 5 }], "id", 1, item => ({ ...item, x: item.x * 2 }))
      // => [{ id: 1, x: 10 }]