Skip to content

Commit 4287531

Browse files
authored
Update deepCompare.js
1 parent 5091ef9 commit 4287531

File tree

1 file changed

+9
-16
lines changed

1 file changed

+9
-16
lines changed

deepCompare_7/deepCompare.js

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
11
//deep comparison of multi-dimensional arrays with recursion
22

33
const deepCompare = (a, b) => {
4-
let res = true
4+
if(a === b) return true
55

6-
if(Array.isArray(a) && Array.isArray(b))
6+
if(typeof a === "object" && typeof b === "object")
77
{
8-
let count = 0;
9-
10-
while(res && count < a.length)
8+
if(a instanceof Date && b instanceof Date) return a.valueOf() === b.valueOf()
9+
else
1110
{
12-
res = !a.some( (val, i) => {
13-
return !deepCompare(val, b[i])
11+
const keysA = Object.keys(a)
12+
if(keysA.length !== Object.keys(b).length) return false
13+
return !keysA.some( key => {
14+
return !deepCompare(a[key], b[key])
1415
})
15-
16-
count++
1716
}
1817
}
19-
else
20-
{
21-
res = (a === b)
22-
}
23-
24-
return res
25-
18+
else return false
2619
}

0 commit comments

Comments
 (0)