-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.html
65 lines (56 loc) · 1016 Bytes
/
test.html
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
function find(arr, f) {
return arr.filter(f)[0];
}
function deepCopy(obj, cache = []) {
if (obj === null || typeof obj !== 'object') {
return obj;
}
const hit = find(cache, function(c) {
if (c.original === obj) {
return true;
} else {
return false;
}
});
if (hit) {
return hit.copy;
}
const copy = Array.isArray(obj) ? [] : {};
cache.push({
original: obj,
copy
});
Object.keys(obj).forEach(key => {
copy[key] = deepCopy(obj[key], cache);
});
return copy;
}
let info = {
name: 'kkk',
age: 18,
address: 'shanghai',
list: [
{ id: 1, title: 'hello' },
{ id: 2, title: 'how' },
{ id: 3, title: 'are' },
{ id: 4, title: 'you' }
],
course: {
school: {
name: 'A',
className: 'B'
}
}
};
let res = deepCopy(info);
console.log(res);
</script>
</body>
</html>