Skip to content

Instantly share code, notes, and snippets.

@sksundram
Last active February 22, 2019 20:04
Show Gist options
  • Save sksundram/b59c4d3b72f020d22cfe41f411b69dc1 to your computer and use it in GitHub Desktop.
Save sksundram/b59c4d3b72f020d22cfe41f411b69dc1 to your computer and use it in GitHub Desktop.
How to copy an array in JavaScript

Copy an array

const persons = [
  { id: 1, name: 'Shailesh', age: 29 },
  { id: 2, name: 'Prem', age: 37 },
  { id: 3, name: 'Mridula', age: 41 }
];

const copy1 = persons.slice();
const copy2 = [...persons];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment