Skip to content

Latest commit

 

History

History
35 lines (26 loc) · 978 Bytes

modifyById.md

File metadata and controls

35 lines (26 loc) · 978 Bytes

modifyById (source code)

  • Curried: true
  • Failsafe status: alternative available

The modifyById function applies a modifier function to the item with the specified id in an array and returns a new array with the modified item in the same index.

Arguments:

  • id: The id of the object to be modified.
  • modifier: A modifier function to modify required properties of the object.
  • entityArray: The array of objects in which the object with given id will be modified using the modifier function.

Usage:

const array = [
  { id: 1, name: "Sam" },
  { id: 2, name: "Oliver" },
];
const idOfItemToBeModified = 2;
const modifier = item => assoc("name", item.name.toUpperCase(), item);

modifyById(idOfItemToBeModified, modifier, array);
/*
[{ id: 1, name: "Sam" }, { id: 2, name: "OLIVER" }]
*/

See also