Created
October 6, 2017 11:38
-
-
Save tejuafonja/1a0b79127cc4b4a15aae2102a3e186e4 to your computer and use it in GitHub Desktop.
How to copy an object or array
This file contains 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
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