Deep Copy an Array or Object


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function deepCopy(obj) {
  2. if (Object.prototype.toString.call(obj) === '[object Array]') {
  3. var out = [], i = 0, len = obj.length;
  4. for ( ; i < len; i++ ) {
  5. out[i] = arguments.callee(obj[i]);
  6. }
  7. return out;
  8. }
  9. if (typeof obj === 'object') {
  10. var out = {}, i;
  11. for ( i in obj ) {
  12. out[i] = arguments.callee(obj[i]);
  13. }
  14. return out;
  15. }
  16. return obj;
  17. }

URL: http://james.padolsey.com/javascript/deep-copying-of-objects-and-arrays/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.