Monday, June 29, 2015

javascript Clone Arrays

To do a shallow copy, array.slice works

http://davidwalsh.name/javascript-clone-array
http://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/slice

slice does not alter. It returns a shallow copy of elements from the original array. Elements of the original array are copied into the returned array as follows:
  • For object references (and not the actual object), slice copies object references into the new array. Both the original and new array refer to the same object. If a referenced object changes, the changes are visible to both the new and original arrays.
  • For strings and numbers (not String and Number objects), slice copies strings and numbers into the new array. Changes to the string or number in one array does not affect the other array.
If you want a deep copy, using JQuery extend

JQuery extend can also merge objects properties
http://api.jquery.com/jquery.extend/

No comments:

Post a Comment