Function replace

Replace an element in an array with a new item without modifying the array and return the new value

const fish = [
{
name: 'Marlin',
weight: 105
},
{
name: 'Bass',
weight: 8
},
{
name: 'Trout',
weight: 13
}
]

const salmon = {
name: 'Salmon',
weight: 22
}

replace(fish, salmon, f => f.name === 'Bass') // => [marlin, salmon, trout]
  • Type Parameters

    • T

    Parameters

    • list: readonly T[]
    • newItem: T
    • match: ((item: T, idx: number) => boolean)
        • (item, idx): boolean
        • Parameters

          • item: T
          • idx: number

          Returns boolean

    Returns T[]