function copy(obj) { var copyObj = Array.isArray(obj) ? [] : {}; for (var key in obj) { if (obj.hasOwnProperty(key)) { if ("object"===typeof obj[key] && obj[key] !== null) copyObj[key] = copy(obj[key]); else copyObj[key] = obj[key]; } } return copyObj; }