-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter04-3-deepcompare.js
88 lines (77 loc) · 2.04 KB
/
chapter04-3-deepcompare.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
var allAreObjects = (...input) => {
for (let item of input) {
if (!isObject(item)) {
return false
}
}
return true;
}
var isObject = (input) => {
return typeof(input) === 'object';
}
/*
Write a function deepEqual that takes two values and
returns true only if they are the same value or are
objects with the same properties, where the values of the
properties are equal when compared with a recursive call
to deepEqual.
To find out whether values should be compared directly (use
the === operator for that) or have their properties
compared, you can use the typeof operator. If it produces
"object" for both values, you should do a deep
comparison.But you have to take one silly exception into
account: because of a historical accident, typeof null
also produces "object".
*/
function deepEqual(data1, data2) {
// if at most one of them is object, compare directly
// if at lease one of them is null, compare directly
// if both non-null object, use deepEqual to check each key-value
if (!allAreObjects(data1, data2)) {
return data1 === data2;
} else {
if (data1 == null || data2 == null) {
return data1 === data2;
} else {
const keys = Object.keys(data1);
for (const i in keys) {
console.log('key deepcompare');
const key = keys[i];
if (!deepEqual(data1[key], data2[key])) {
return false;
}
// only exit when key deepcompare returns false
// continue comparing other keys when deepcampare true
}
return true;
}
}
}
/* Accessing Data in Object
- obj.key would use key as string literal "key"
- obj[key] would recognize key as an expression
*/
// Test for Deep Comparison
let obj = {
here: {
is: "an"
},
object: 2
};
console.log('*'.repeat(12));
console.log(deepEqual(obj, obj));
// → true
console.log('*'.repeat(12));
console.log(deepEqual(obj, {
here: 1,
object: 2
}));
// → false
console.log('*'.repeat(12));
console.log(deepEqual(obj, {
here: {
is: "an"
},
object: 2
}));
// → true