-
Notifications
You must be signed in to change notification settings - Fork 0
/
eqObjects.js
48 lines (43 loc) · 1.23 KB
/
eqObjects.js
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
44
45
46
47
48
// Returns true if both objects have identical keys with identical values.
// Otherwise you get back a big fat false!
// const eqObjects = function (object1, object2) {
// const keys1 = Object.keys(object1);
// const keys2 = Object.keys(object2);
// if (keys1.length !== keys2.length) {
// return false;
// }
// for (let key of keys1) {
// if (Array.isArray(object1[key])) {
// if (!eqArrays(object1[key], object2[key])) {
// return false;
// }
// } else if (object1[key] !== object2[key]) {
// return false;
// }
// }
// return true;
// };
// refactor eqobject
const eqObjects = function(obj1, obj2) {
// base case
if (obj1 === obj2) return true;
// check null and type
if (obj1 === null || obj2 === null || typeof obj1 !== "object" || typeof obj2 !== "object") return false;
// get the keys
let obj1Keys = Object.keys(obj1);
let obj2Keys = Object.keys(obj2);
// check the length of keys
if (obj1Keys.length !== obj2Keys.length) return false;
for (let key of obj1Keys) {
//
if (!obj2Keys.includes(key)) {
return false;
}
// recursive case
if (!eqObjects(obj1[key],obj2[key])) {
return false;
}
}
return true;
};
module.exports = eqObjects;