Created
April 11, 2018 20:56
-
-
Save yosevu/b7ddc001622385d30c98b9c4fe8e7741 to your computer and use it in GitHub Desktop.
Object spread with updated properties
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
| // TIL: When you update an object's properties with the Object spread operator, put the updated properties | |
| // before spreading the object so that the property you are updating isn't reassigned to its original value. | |
| const meat = { | |
| options: [ | |
| 'crocodile', | |
| 'wildebeest', | |
| 'ostrich', | |
| 'warthog', | |
| 'antelope', | |
| ], | |
| isRequired: true, | |
| }; | |
| const newMeat1 = { | |
| ...meat, | |
| isRequired: false, | |
| }; | |
| console.log(newMeat1.isRequired); // false | |
| const newMeat2 = { | |
| isRequired: false, | |
| ...meat, | |
| }; | |
| console.log(newMeat2.isRequired); // true | |
| // const newOptions1 = [ | |
| // ...meat.options, | |
| // 'wildebeest', | |
| // ]; | |
| // const newOptions2 =[ | |
| // 'wildebeest', | |
| // ...meat.options, | |
| // ]; | |
| // console.log(newOptions1); | |
| // console.log(newOptions2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment