Skip to content

Instantly share code, notes, and snippets.

@ricardocanelas
Created January 2, 2017 12:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ricardocanelas/f6dc29050f6757a785b0d7d9875a9f6e to your computer and use it in GitHub Desktop.
Save ricardocanelas/f6dc29050f6757a785b0d7d9875a9f6e to your computer and use it in GitHub Desktop.
Javascript / Copying Arrays and Objects
// 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