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.
item[key] === value
update
The source array.
The key to match on.
The value to match.
Partial object or updater function.
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 }] Copy
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 }]
Returns a new array where every item matching
item[key] === valueis updated.updatecan be a partial object (merged with the item) or a function that returns a new item.