Skip to content

Instantly share code, notes, and snippets.

@tejuafonja
Created October 6, 2017 11:38
Show Gist options
  • Save tejuafonja/1a0b79127cc4b4a15aae2102a3e186e4 to your computer and use it in GitHub Desktop.
Save tejuafonja/1a0b79127cc4b4a15aae2102a3e186e4 to your computer and use it in GitHub Desktop.
How to copy an object or array
var player = {score: 1, name: 'Jeff'};
var newPlayer = Object.assign({}, player, {score: 2});
// Now player is unchanged, but newPlayer is {score: 2, name: 'Jeff'}
// Or if you are using object spread syntax proposal, you can write:
var newPlayer = {...player, score: 2};
// for Arrays
var player = ['Ronaldo', 'Merci', 'Rooney']
var newPlayer = player.slice()
newPlayer.push('Jeff')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment