Skip to content

Latest commit

 

History

History
33 lines (24 loc) · 957 Bytes

replaceBy.md

File metadata and controls

33 lines (24 loc) · 957 Bytes

replaceBy (source code)

  • Curried: true
  • Failsafe status: alternative available

The replaceBy function replaces all items in the array that match the provided pattern with the given item.

Arguments:

  • pattern: The pattern using which the objects will be matched.
  • newItem: The object with which the matched objects need to be replaced.
  • entityArray: The array of objects in which the objects with the given pattern will be replaced.

Usage:

const array = [
  { id: 1, name: "Sam", address: { street: "First street", pin: 101283 } },
  { id: 2, name: "Oliver", address: { street: "Second street", pin: 998472 } },
];
const newItem = { id: 2, name: "John" };

replaceBy({ name: "Oliver" }, newItem, array);
/*
[{id: 1, name: "Sam"}, {id: 2, name: "John"}]
 */

See also