Created
January 2, 2017 12:03
-
-
Save ricardocanelas/f6dc29050f6757a785b0d7d9875a9f6e to your computer and use it in GitHub Desktop.
Javascript / Copying Arrays and Objects
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
// Array | |
const players = ['Ricardo', 'Canelas', 'Robyn', 'Emma']; | |
const team = players.slice(); | |
const team2 = [].concat(players); | |
const team3 = [...players]; // ...spread | |
// Object - *note: this is only 1 level deep | |
const person = {name: 'Ricardo Canelas', age: 23}; | |
const capitan = Object.assign({}, person, { from: 'Brazil' }); | |
// Object - *note: for many levels deep | |
const person = {name: 'Ricardo Canelas', age: 23, social:{twitter:'ricardocanelas'}; | |
const developer = JSON.parse(JSON.stringify(person)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment