Skip to content

Instantly share code, notes, and snippets.

@pokorson
Created October 5, 2018 11:08
Show Gist options
  • Save pokorson/4c2771f82c843137e038bcaa9c321236 to your computer and use it in GitHub Desktop.
Save pokorson/4c2771f82c843137e038bcaa9c321236 to your computer and use it in GitHub Desktop.
Javascript shallow copying example
// Example of updating record on shallow copied array in Javascript
const arr1 = [{a: 10, b: 15}];
console.log("arr1 before update = ", arr1);
const arr2 = arr1.slice();
console.log("arr2 before update = ", arr2);
arr2[0].b = 15;
console.log("arr2 after update = ", arr2);
console.log("arr1 after update = ", arr1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment