Last active
September 27, 2019 15:42
-
-
Save NyaGarcia/d5b120da2dd99740cd69f3ca7a58f17a to your computer and use it in GitHub Desktop.
Cloning an object with nested array, with the spread operator
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 pokemon = { | |
name: 'Squirtle', | |
type: 'Water', | |
abilities: ['Torrent', 'Rain Dish'] | |
}; | |
const squirtleClone = { ...pokemon, abilities: [...pokemon.abilities] }; | |
pokemon.name = 'Charmander'; | |
pokemon.abilities.push('Surf'); | |
console.log(squirtleClone); //Result: { name: 'Squirtle', type: 'Water', abilities: [ 'Torrent', 'Rain Dish' ] } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment