forked from HackYourFuture/JavaScript1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathequal.js
More file actions
43 lines (36 loc) · 810 Bytes
/
equal.js
File metadata and controls
43 lines (36 loc) · 810 Bytes
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
38
39
40
41
42
43
const obj1 = {
a: '1',
b: 'this is the letter b',
f: {
foo: 'what is a foo anyway',
bar: [1, 5, '3', 4]
}
};
const obj2 = {
a: 1,
b: 'this is the letter b',
f: {
foo: 'what is a foo anyway',
bar: [1, 5, 3, 4]
}
};
function equal(a, b, mode) {
const eq = mode === 'strict' ? a === b : a == b;
if (eq) {
return true;
}
if (a && b &&
typeof a === 'object' &&
typeof b === 'object' &&
Object.keys(a).length === Object.keys(b).length) {
for (const key of Object.keys(a)) {
if (!b.hasOwnProperty(key) || !equal(a[key], b[key], mode)) {
return false;
}
}
return true;
}
return false;
};
console.log('objects are equal: ' + equal(obj1, obj2));
console.log('objects are strictly equal: ' + equal(obj1, obj2, 'strict'));