Last active
April 26, 2024 03:29
-
-
Save liunian/d7eab5a01ccb0adb9a744d1680ceab6e to your computer and use it in GitHub Desktop.
clone json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// from: https://jsperf.com/deep-copy-vs-json-stringify-json-parse/15 | |
function recursiveDeepCopy(o) { | |
var newO, | |
i; | |
if (typeof o !== 'object') { | |
return o; | |
} | |
if (!o) { | |
return o; | |
} | |
if ('[object Array]' === Object.prototype.toString.apply(o)) { | |
newO = []; | |
for (i = 0; i < o.length; i += 1) { | |
newO[i] = recursiveDeepCopy(o[i]); | |
} | |
return newO; | |
} | |
newO = {}; | |
for (i in o) { | |
if (o.hasOwnProperty(i)) { | |
newO[i] = recursiveDeepCopy(o[i]); | |
} | |
} | |
return newO; | |
} | |
function recursiveDeepCopyNoTouch(o) { | |
var i; | |
if (typeof o !== 'object') { | |
return o; | |
} | |
if (!o) { | |
return o; | |
} | |
if ('[object Array]' === Object.prototype.toString.apply(o)) { | |
for (i = 0; i < o.length; i += 1) { | |
recursiveDeepCopy(o[i]); | |
} | |
return o; | |
} | |
for (i in o) { | |
if (o.hasOwnProperty(i)) { | |
recursiveDeepCopy(o[i]); | |
} | |
} | |
return o; | |
} | |
function deepCopy(o) { | |
var copy = o, | |
k; | |
if (o && typeof o === 'object') { | |
copy = Object.prototype.toString.call(o) === '[object Array]' ? [] : {}; | |
for (k in o) { | |
copy[k] = deepCopy(o[k]); | |
} | |
} | |
return copy; | |
} | |
function clone(obj) { | |
if (obj == null || typeof(obj) != 'object') | |
return obj; | |
var temp = new obj.constructor(); | |
for (var key in obj) | |
temp[key] = clone(obj[key]); | |
return temp; | |
} | |
var uc = { | |
"list": { | |
"0oVwOM": { | |
"id": "0oVwOM", | |
"parent": "pTlmbh", | |
"name": "New node", | |
"created_at": 1384289621 | |
}, | |
"aHxe8k": { | |
"id": "aHxe8k", | |
"parent": "Fhs2hL", | |
"name": "hjkhjkhjk", | |
"created_at": 1384354593 | |
}, | |
"Fhs2hL": { | |
"id": "Fhs2hL", | |
"parent": "root", | |
"name": "test", | |
"created_at": 1383403881 | |
}, | |
"HYPSgv": { | |
"id": "HYPSgv", | |
"parent": "0oVwOM", | |
"name": "New node", | |
"created_at": 1384342657 | |
}, | |
"osFIMf": { | |
"id": "osFIMf", | |
"parent": "root", | |
"name": "New node", | |
"created_at": 1384354584 | |
}, | |
"PsovXE": { | |
"id": "PsovXE", | |
"parent": "root", | |
"name": "hjkhjkhjk", | |
"created_at": 1384354589 | |
}, | |
"pTlmbh": { | |
"id": "pTlmbh", | |
"parent": "Fhs2hL", | |
"name": "New node", | |
"created_at": 1384289277 | |
}, | |
"RbXhdJ": { | |
"id": "RbXhdJ", | |
"parent": "root", | |
"name": "empty", | |
"created_at": 1384359806 | |
} | |
}, | |
"maps": { | |
"parent": { | |
"pTlmbh": { | |
"0oVwOM": { | |
"id": "0oVwOM", | |
"parent": "pTlmbh", | |
"name": "New node", | |
"created_at": 1384289621 | |
} | |
}, | |
"Fhs2hL": { | |
"aHxe8k": { | |
"id": "aHxe8k", | |
"parent": "Fhs2hL", | |
"name": "hjkhjkhjk", | |
"created_at": 1384354593 | |
}, | |
"pTlmbh": { | |
"id": "pTlmbh", | |
"parent": "Fhs2hL", | |
"name": "New node", | |
"created_at": 1384289277 | |
} | |
}, | |
"root": { | |
"Fhs2hL": { | |
"id": "Fhs2hL", | |
"parent": "root", | |
"name": "test", | |
"created_at": 1383403881 | |
}, | |
"osFIMf": { | |
"id": "osFIMf", | |
"parent": "root", | |
"name": "New node", | |
"created_at": 1384354584 | |
}, | |
"PsovXE": { | |
"id": "PsovXE", | |
"parent": "root", | |
"name": "hjkhjkhjk", | |
"created_at": 1384354589 | |
}, | |
"RbXhdJ": { | |
"id": "RbXhdJ", | |
"parent": "root", | |
"name": "empty", | |
"created_at": 1384359806 | |
} | |
}, | |
"0oVwOM": { | |
"HYPSgv": { | |
"id": "HYPSgv", | |
"parent": "0oVwOM", | |
"name": "New node", | |
"created_at": 1384342657 | |
} | |
} | |
}, | |
"path": [ | |
["Fhs2hL"], | |
["Fhs2hL", "aHxe8k"], | |
["Fhs2hL", "pTlmbh"], | |
["Fhs2hL", "pTlmbh", "0oVwOM"], | |
["Fhs2hL", "pTlmbh", "0oVwOM", "HYPSgv"], | |
["osFIMf"], | |
["PsovXE"], | |
["RbXhdJ"] | |
] | |
} | |
}; | |
var isPlainObject = function (obj) { | |
return obj != null && typeof obj === 'object' | |
&& !Array.isArray(obj) && obj.constructor === Object; | |
} | |
var trackable = function (o, stats, prependKey) { | |
var out, key; | |
var stats = stats || []; | |
var prependKey = prependKey || ''; | |
if (typeof o !== 'object') { | |
return o; | |
} | |
if (!o) { | |
return o; | |
} | |
if ('[object Array]' === Object.prototype.toString.apply(o)) { | |
// ignore arrays | |
return JSON.parse(JSON.stringify(o)); | |
} | |
out = {}; | |
var each = function (v, k) { | |
Object.defineProperty(out, k, { | |
enumerable: true, | |
get: function () { | |
stats.push(catKey); | |
return v; | |
} | |
}); | |
}; | |
for (key in o) { | |
if (o.hasOwnProperty(key)) { | |
var catKey = (prependKey ? prependKey + '.' : '') + key; | |
var value = trackable(o[key], stats, catKey); | |
each(value, key); | |
} | |
} | |
if (!prependKey) { | |
Object.defineProperty(out, 'stats', { | |
enumerable: true, | |
value: function () { | |
return stats; | |
} | |
}); | |
} | |
return out; | |
}; | |
function recursive(obj) { | |
var clone, i; | |
if (typeof obj !== 'object' || !obj) | |
return obj; | |
if ('[object Array]' === Object.prototype.toString.apply(obj)) { | |
clone = []; | |
var len = obj.length; | |
for (i = 0; i < len; i++) | |
clone[i] = recursive(obj[i]); | |
return clone; | |
} | |
clone = {}; | |
for (i in obj) | |
if (obj.hasOwnProperty(i)) | |
clone[i] = recursive(obj[i]); | |
return clone; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment