8

I'm trying to make a function that duplicates an array of arrays. I tried blah.slice(0); but it only copies the references. I need to make a duplicate that leaves the original intact.

I found this prototype method at http://my.opera.com/GreyWyvern/blog/show.dml/1725165

Object.prototype.clone = function() {
  var newObj = (this instanceof Array) ? [] : {};
  for (i in this) {
    if (i == 'clone') continue;
    if (this[i] && typeof this[i] == "object") {
      newObj[i] = this[i].clone();
    } else newObj[i] = this[i]
  } return newObj;
};

It works, but messes up a jQuery plugin I'm using - so I need to turn it onto a function... and recursion isn't my strongest.

Your help would be appreciated!

Cheers,

Jeremy
  • 925
  • 2
  • 11
  • 22

2 Answers2

5
function clone (existingArray) {
   var newObj = (existingArray instanceof Array) ? [] : {};
   for (i in existingArray) {
      if (i == 'clone') continue;
      if (existingArray[i] && typeof existingArray[i] == "object") {
         newObj[i] = clone(existingArray[i]);
      } else {
         newObj[i] = existingArray[i]
      }
   }
   return newObj;
}
Churk
  • 4,556
  • 5
  • 22
  • 37
2

For example:

clone = function(obj) {
    if (!obj || typeof obj != "object")
        return obj;
    var isAry = Object.prototype.toString.call(obj).toLowerCase() == '[object array]';
    var o = isAry ? [] : {};
    for (var p in obj)
        o[p] = clone(obj[p]);
    return o;
}

improved as per comments

georg
  • 211,518
  • 52
  • 313
  • 390
  • 1
    Will break for `null` (null.pop will throw). The first check should be something like `if (typeof obj != "object" || !obj)`. – Alexander Pavlov Feb 22 '12 at 17:06
  • Also, questionable handling of inherited properties - "clone" in JavaScript has problematic semantics. – Pointy Feb 22 '12 at 17:07