Skip to content

Instantly share code, notes, and snippets.

@obiwankenoobi
Last active September 24, 2018 08:47
Show Gist options
  • Save obiwankenoobi/219f83f4a7e44d772e8666c07c7f30bd to your computer and use it in GitHub Desktop.
Save obiwankenoobi/219f83f4a7e44d772e8666c07c7f30bd to your computer and use it in GitHub Desktop.
function to deep copying an array of objects
const copyArr = arr => arr.map(obj => JSON.parse(JSON.stringify(obj)));
/*
** %%%%%%%%%%%%EXAMPLE%%%%%%%%%%%%%%%
** let arr = [
** { name: "arty" },
** { name: "yotam" },
** [1,2,3]
** ];
** let coppyArr = deepCopiedArr(arr);
** coppyArr[0].name = 'not arty';
** coppyArr[2] = [3,2,1];
**
**
** console.log(arr[0].name); // 'arty'
** console.log(arr[2]); // '[1,2,3]'
** %%%%%%%%%%%%EXAMPLE%%%%%%%%%%%%%%%
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment