-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
2628-json-deep-equal.js
39 lines (38 loc) · 986 Bytes
/
2628-json-deep-equal.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
/**
* @param {any} o1
* @param {any} o2
* @return {boolean}
*/
var areDeeplyEqual = function(o1, o2) {
if (o1 === null || o2 === null) {
return o1 === o2;
}
if (typeof o1 !== typeof o2) {
return false;
}
if (typeof o1 !== 'object') { // primitives
return o1 === o2;
}
if (Array.isArray(o1) && Array.isArray(o2)) { // Arrays
if (o1.length !== o2.length) {
return false;
}
for (let i = 0; i < o1.length; i++) {
if (!areDeeplyEqual(o1[i], o2[i])) {
return false;
}
}
} else if (!Array.isArray(o1) && !Array.isArray(o2)) { // Objects
if (Object.keys(o1).length !== Object.keys(o2).length) {
return false;
}
for (const key in o1) {
if (!areDeeplyEqual(o1[key], o2[key])) {
return false;
}
}
} else {
return false;
}
return true;
};