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

    Function replaceArrayItemByKey

    • Returns a new array where every item matching item[key] === value is replaced. replacement can be a new item or a function that receives the matched item and returns one.

      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.

      • replacement: T | ((item: T) => T)

        Replacement item or factory function.

      Returns T[]

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

      replaceArrayItemByKey([{ id: 1, x: 0 }], "id", 1, item => ({ ...item, x: item.x + 1 }))
      // => [{ id: 1, x: 1 }]