Skip to content

Instantly share code, notes, and snippets.

@darleykrefta
Created March 20, 2021 13:30
Show Gist options
  • Save darleykrefta/4ea4431d9b88e22b6a6f5ecc920f2d4a to your computer and use it in GitHub Desktop.
Save darleykrefta/4ea4431d9b88e22b6a6f5ecc920f2d4a to your computer and use it in GitHub Desktop.
const droid = {
  name: 'R2-D2',
  height: '1.08',
  colors: ['blue', 'silver']
}

const newDroid = {
  ...droid,
  colors: [...droid.colors, 'white']
}

//the original object is not updated
console.log(droid)
// [object Object] {
//   colors: ['blue', 'silver'],
//   height: '1.08',
//   name: 'R2-D2'
// }

// we created a totally new object
console.log(newDroid)
// [object Object] {
//   colors: ['blue', 'silver', 'white'],
//   height: '1.08',
//   name: 'R2-D2'
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment