-
Notifications
You must be signed in to change notification settings - Fork 29
/
clone_object.js
49 lines (44 loc) · 900 Bytes
/
clone_object.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
var _ = require("lodash");
var obj1 = {
a: {
b: {
c: {
d: {
e: {
name: "alsotang",
at: "china",
},
},
},
},
},
};
// !!!!NOTICE
// According to what kind of element you want,
// result of the three is not always the same.
/*
the JSON approach doesn't work with objects well (like date, regexp, string object, functions).
Lodash handles them as well as typed arrays and array buffers.
-- by @jdalton
*/
const cases = [
{
name: "JSON.parse(JSON.stringify)",
fn: function () {
var obj2 = JSON.parse(JSON.stringify(obj1));
},
},
{
name: "lodash.cloneDeep",
fn: function () {
var obj2 = _.cloneDeep(obj1);
},
},
{
name: "lodash.clone. this is shallow clone",
fn: function () {
var obj2 = _.clone(obj1);
},
},
];
exports = module.exports = { cases };