Skip to content

Commit 07b93a0

Browse files
authored
Update README.md
1 parent 040a452 commit 07b93a0

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

README.md

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -593,23 +593,30 @@ 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
598598
```js if(typeof a === "object" && typeof b === "object")...```
599599
600-
Next, we can process the dates first, as that doesn't require iteration. Make sure to compare ```Date.valueOf()``` instead of the date itself.
600+
Note that we use ```===``` here to differentiate between data types strictly.
601+
602+
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.
601603
602604
```js if(a instanceof Date && b instanceof Date) return a.valueOf() === b.valueOf() ```
603605
604606
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.
605607
606608
```js
607-
const keysA = Object.keys(a) //get keys/index of object/array
608-
if(keysA.length !== Object.keys(b).length) return false //make sure a and b are same length
609+
//get keys/index of object/array a
610+
const keysA = Object.keys(a)
611+
612+
//make sure a and b are the same length
613+
if(keysA.length !== Object.keys(b).length) return false
609614

610-
//Array.some stops executing nested code the moment there is one different value
611-
return !keysA.some( key => {
612-
return !deepCompare(a[key], b[key]) //run deepCompare recursively
615+
//Array.some() iterates through the values in an array and stops executing nested code until there is one that returns true
616+
//in this case that would be when there is one different value between a and b
617+
return !keysA.some( key => {
618+
//run deepCompare recursively
619+
return !deepCompare(a[key], b[key])
613620
})
614621
```
615622
@@ -635,9 +642,18 @@ const deepCompare = (a, b) => {
635642
else return false
636643
}
637644

638-
deepCompare(1, 2) //false
645+
deepCompare(1, 2)
646+
//false
647+
648+
deepCompare({"first": 1, "second": 2}, {"first": 1, "second": 2})
649+
//true
650+
651+
deepCompare([1, "2", 3.0], [1, "2", 3])
652+
//false
653+
639654
const arr = [1, 2, "3", [{4: "5", 6: 7}, 8.0, new Date(), undefined]]
640-
deepCompare(arr, [1, 2, "3", [{4: "5", 6: 7}, 8.0, new Date(), undefined]]) //true
655+
deepCompare(arr, [1, 2, "3", [{4: "5", 6: 7}, 8.0, new Date(), undefined]])
656+
//true
641657

642658
```
643659

0 commit comments

Comments
 (0)