replaceById (source code)
- Curried: true
- Failsafe status: alternative available
The replaceById
function returns a new array with the item having the
specified id replaced by the provided object.
id
: The id of the object to be replaced.newItem
: The new object to replace.entityArray
: The array of objects in which the object with given id will be replaced with the given object.
const array = [
{ id: 1, name: "Sam" },
{ id: 2, name: "Oliver" },
];
const idOfItemToBeReplaced = 2;
const newItem = { id: 3, name: "John" };
replaceById(idOfItemToBeReplaced, newItem, array);
/*
[{ id: 1, name: "Sam" }, { id: 3, name: "John" }]
*/