Skip to content

Instantly share code, notes, and snippets.

@asherccohen
Created December 17, 2019 12:45
Show Gist options
  • Save asherccohen/ddaf6599cad4f6ae4ac40f8d744bd6f4 to your computer and use it in GitHub Desktop.
Save asherccohen/ddaf6599cad4f6ae4ac40f8d744bd6f4 to your computer and use it in GitHub Desktop.
Use spread operator to change an element in an array of objects
const array = [
{ id: 1,
name: 'Bob' },
{ id: 2,
name: 'Lucy' },
{ id: 3,
name: 'John' }
];
const [first, ...rest] = array;
const newArray = [
{...first,
name: 'Mark'},
...rest,
];
console.log(newArray[0].name); // Mark
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment