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.
item[key] === value
replacement
The source array.
The key to match on.
The value to match.
Replacement item or factory function.
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 }] Copy
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 }]
Returns a new array where every item matching
item[key] === valueis replaced.replacementcan be a new item or a function that receives the matched item and returns one.