Skip to content

Instantly share code, notes, and snippets.

@yosevu
Created April 11, 2018 20:56
Show Gist options
  • Select an option

  • Save yosevu/b7ddc001622385d30c98b9c4fe8e7741 to your computer and use it in GitHub Desktop.

Select an option

Save yosevu/b7ddc001622385d30c98b9c4fe8e7741 to your computer and use it in GitHub Desktop.
Object spread with updated properties
// 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