Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript update array of objects with reduce site:stackoverflow.com

function upsert(array, element) { // (1)
  const i = array.findIndex(_element => _element.id === element.id);
  if (i > -1) array[i] = element; // (2)
  else array.push(element);
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #javascript #update #array #objects #reduce
ADD COMMENT
Topic
Name
6+7 =