-
Notifications
You must be signed in to change notification settings - Fork 0
/
eqObjects.js
35 lines (32 loc) · 1.13 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
const eqArrays = require("./eqArrays");
// 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);
// check if length of both object keys are the same (i.e check if key for both objects are equal)
if (keys1.length !== keys2.length) {
return false;
}
//loop through keys
for (key of keys1) {
console.log(key);
// check if values of both objects have an array (nested)
if (Array.isArray(object1[key]) && Array.isArray(object2[key])) {
// if eqArrays (checking if both arrays are equal) returns is false, return false
if (!eqArrays(object1[key], object2[key])) {
return false;
}
// return array check function to see if arrays are matching (nested arrays)
// return eqArrays(object1[key], object2[key]);
} else {
// if any values do not match
if (object1[key] !== object2[key]) {
// return false
return false;
}
}
}
return true;
};
module.exports = eqObjects;