Skip to content

Instantly share code, notes, and snippets.

@NyaGarcia
Last active September 27, 2019 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NyaGarcia/d5b120da2dd99740cd69f3ca7a58f17a to your computer and use it in GitHub Desktop.
Save NyaGarcia/d5b120da2dd99740cd69f3ca7a58f17a to your computer and use it in GitHub Desktop.
Cloning an object with nested array, with the spread operator
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