Skip to content

Latest commit

 

History

History
34 lines (25 loc) · 856 Bytes

replaceById.md

File metadata and controls

34 lines (25 loc) · 856 Bytes

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.

Arguments:

  • 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.

Usage:

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" }]
*/

See also