Skip to content

Instantly share code, notes, and snippets.

@aherve
Created August 30, 2017 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aherve/5495b52f457090dedad91e210fb0f18e to your computer and use it in GitHub Desktop.
Save aherve/5495b52f457090dedad91e210fb0f18e to your computer and use it in GitHub Desktop.
// update an object in array by property
const initial = [ {id: 1, score: 1}, {id: 2, score: 2}, {id: 3, score: 4}]
const newValue = {id: 3, score: 3}
const updated = initial.map(x => x.id === newValue.id ? newValue : x)
console.log(updated) // => [ { id: 1, score: 1 }, { id: 2, score: 2 }, { id: 3, score: 3 } ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment