-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.ts
34 lines (34 loc) · 893 Bytes
/
Solution.ts
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
function areDeeplyEqual(o1: any, o2: any): boolean {
if (o1 === null || typeof o1 !== 'object') {
return o1 === o2;
}
if (typeof o1 !== typeof o2) {
return false;
}
if (Array.isArray(o1) !== Array.isArray(o2)) {
return false;
}
if (Array.isArray(o1)) {
if (o1.length !== o2.length) {
return false;
}
for (let i = 0; i < o1.length; i++) {
if (!areDeeplyEqual(o1[i], o2[i])) {
return false;
}
}
return true;
} else {
const keys1 = Object.keys(o1);
const keys2 = Object.keys(o2);
if (keys1.length !== keys2.length) {
return false;
}
for (const key of keys1) {
if (!areDeeplyEqual(o1[key], o2[key])) {
return false;
}
}
return true;
}
}