-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheqObjects.js
50 lines (41 loc) · 1.29 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
49
50
//Import function
const assertEqual = require('./assertEqual');
const eqArrays = require('./eqArrays');
// Function to compare objects
const eqObjects = function(object1, object2) {
let result;
// They have the same number of keys
if (Object.keys(object1).length === Object.keys(object2).length) {
for (const key in object1) {
//The value for each key in one object is the same as the value for that same key in the other object
if (Array.isArray(object1[key]) && Array.isArray(object2[key])) {
if (eqArrays(object1[key], object2[key])) {
result = true;
} else {
result = false;
break;
}
} else if (object1[key] === object2[key]) {
result = true;
} else {
result = false;
break;
}
}
} else {
result = false;
}
return result;
};
module.exports = eqObjects;
// TEST CODE
const ab = { a: "1", b: "2" };
const ba = { b: "2", a: "1" };
assertEqual(eqObjects(ab, ba), true); // => true
const abc = { a: "1", b: "2", c: "3" };
assertEqual(eqObjects(ab, abc), false); // => false
const cd = { c: "1", d: ["2", 3] };
const dc = { d: ["2", 3], c: "1" };
assertEqual(eqObjects(cd, dc), true); // => true
const cd2 = { c: "1", d: ["2", 3, 4] };
assertEqual(eqObjects(cd, cd2), false); // => false