-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeepComparison.js
More file actions
37 lines (32 loc) · 1.36 KB
/
deepComparison.js
File metadata and controls
37 lines (32 loc) · 1.36 KB
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
35
36
37
/*Deep comparison*/
/*
The == operator compares objects by identity. But sometimes, you would prefer to compare the values of their actual
properties.
Write a function, deepEqual, that takes two values and returns true only if they are the same value or are objects with
the same properties whose values are also equal when compared with a recursive call to deepEqual.
To find out whether to compare two things by identity (use the === operator for that) or by looking at their
properties, you can use the typeof operator. If it produces "object" for both values, you should do a deep comparison.
But you have to take one silly exception into account: by a historical accident, typeof null also produces "object".
*/
function deepEqual(obj1, obj2) {
if ((typeof obj1 == "object" && obj1 != null) && (typeof obj2 == "object" && obj2 != null)) {
if (Object.keys(obj1).length != Object.keys(obj2).length) return false;
else {
for (var prop in obj1) {
if (obj2.hasOwnProperty(prop)) {
return (deepEqual(obj1[prop], obj2[prop])) ? true : false;
}
}
}
}
else {
return (obj1 === obj2) ? true : false;
}
}
var obj = {here: {is: "an"}, object: 2};
console.log(deepEqual(obj, obj));
// → true
console.log(deepEqual(obj, {here: 1, object: 2}));
// → false
console.log(deepEqual(obj, {here: {is: "an"}, object: 2}));
// → true