Created
December 7, 2017 21:12
-
-
Save aire-con-gas/169142662f670659d13438d8d512e9bd to your computer and use it in GitHub Desktop.
deepEqual
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const deepEqual = (a, b) => { | |
| const isEqual = (a, b) => (a === b); | |
| const objCompare = (a, b) => { | |
| for(let k in a) { | |
| if (typeof b[k] === 'undefined') { | |
| return false; | |
| } | |
| if (!isEqual(a[k], b[k])) { | |
| return false; | |
| } | |
| } | |
| return true; | |
| } | |
| if (typeof a === 'object' && typeof b === 'object') { | |
| return objCompare(a, b); | |
| } else { | |
| return isEqual(a, b); | |
| } | |
| } | |
| console.log(deepEqual(1, '1')); //false | |
| console.log(deepEqual({a: 'b'}, 1)); //false | |
| console.log(deepEqual({ key: 'value'}, { key: 'value'})); //true | |
| console.log(deepEqual({ key1: 'value1', key2: { key3: 'value3' }}, { key1: 'value1', key2: { key3: 'value3' }})); //false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The last one should return
true