Skip to content

Commit 6d78de2

Browse files
committed
this pr is looking good_merging it
2 parents 24bb584 + acbf60c commit 6d78de2

File tree

1 file changed

+50
-4
lines changed

1 file changed

+50
-4
lines changed

README.md

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -593,14 +593,45 @@ Firstly, we'll do the most simple check of ```a === b``` to avoid unnecessary co
593593
if(a === b) return true
594594
```
595595
596-
Then comes the interesting part! There are several data types we need to look out for: Objects, Arrays(which JS treats as an object), and Dates(which is also treated as an object!), thus all we have to do is check if both a and is type object. If not, we can just return false as they didn't pass the ```a === b``` test.
596+
Then comes the interesting part! There are several data types we need to look out for: Objects, Arrays(which JS treats as an object), and Dates(which is also treated as an object!), thus all we have to do is check if both a and b are of type object. If not, we can just return false as they didn't pass the ```a === b``` test.
597597
598-
Next, we can process the dates first, as that doesn't require iteration. Make sure to compare ```Date.valueOf()``` instead.
598+
```js
599+
if(typeof a === "object" && typeof b === "object")...
600+
```
601+
602+
Note that we use ```===``` here to differentiate between data types strictly.
603+
604+
Next, we can process the dates first, as that doesn't require iteration. Make sure to compare ```Date.valueOf()``` instead of the date object itself.
605+
606+
```js
607+
if(a instanceof Date && b instanceof Date) return a.valueOf() === b.valueOf()
608+
```
599609
600610
Lastly, by taking the keys of the iterables we can compare the length of ```a``` and ```b```, then make use of built-in Array.some method to check if any values of the two iterables don't match.
601611
602612
```js
603-
if(typeof a === "object" && typeof b === "object")
613+
//get keys/index of object/array a
614+
const keysA = Object.keys(a)
615+
616+
//make sure a and b are the same length
617+
if(keysA.length !== Object.keys(b).length) return false
618+
619+
//Array.some() iterates through the values in an array and stops executing nested code until there is one that returns true
620+
//in this case that would be when there is one different value between a and b
621+
return !keysA.some( key => {
622+
//run deepCompare recursively
623+
return !deepCompare(a[key], b[key])
624+
})
625+
```
626+
627+
628+
Put it all together, and we have
629+
630+
```js
631+
const deepCompare = (a, b) => {
632+
if(a === b) return true
633+
634+
if(typeof a === "object" && typeof b === "object")
604635
{
605636
if(a instanceof Date && b instanceof Date) return a.valueOf() === b.valueOf()
606637
else
@@ -613,9 +644,24 @@ if(typeof a === "object" && typeof b === "object")
613644
}
614645
}
615646
else return false
647+
}
648+
649+
deepCompare(1, 2)
650+
//false
651+
652+
deepCompare({"first": 1, "second": 2}, {"first": 1, "second": 2})
653+
//true
654+
655+
deepCompare([1, "2", 3.0], [1, "2", 3])
656+
//false
657+
658+
const arr = [1, 2, "3", [{4: "5", 6: 7}, 8.0, new Date(), undefined]]
659+
deepCompare(arr, [1, 2, "3", [{4: "5", 6: 7}, 8.0, new Date(), undefined]])
660+
//true
661+
616662
```
617663
618-
It's that simple! Hope this helps.
664+
It's that simple! Hope this helps.
619665
</p>
620666
<hr>
621667
<hr>

0 commit comments

Comments
 (0)