Created
December 17, 2019 12:45
-
-
Save asherccohen/ddaf6599cad4f6ae4ac40f8d744bd6f4 to your computer and use it in GitHub Desktop.
Use spread operator to change an element in an array of objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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