Skip to content

Instantly share code, notes, and snippets.

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

const newDroid = { ...droid }

newDroid.colors.push('white')

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

// we created a new object with the same references of nested objects/arrays
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