-
Notifications
You must be signed in to change notification settings - Fork 0
/
eqObjects.js
52 lines (47 loc) · 1.3 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
51
52
const assertEqual = function(actual, expected) {
if (actual === expected) {
console.log(`😙😃😃Assertion Passed: ${actual} === ${expected}`);
} else {
console.log(`😞😞😞Assertion Failed: ${actual} !== ${expected}`);
}
};
const eqArrays = function(x,y) {
let output;
if (Array.isArray(x) && Array.isArray(y)) {
if (x.length !== y.length) {
return false;
}
for (let i = 0; i < x.length; i++) {
if (x[i] === y[i]) {
output = true;
} else {
output = false;
}
}
}
return output;
};
const eqObjects = function(object1, object2) {
if (Object.keys(object1).length !== Object.keys(object2).length) {
return false;
}
for (let k of Object.keys(object1)) {
if (Array.isArray(object1[k]) && Array.isArray(object2[k])) {
return eqArrays(object1[k],object2[k]);
} else {
if (object1[k] !== object2[k]) {
return false;
}
}
} return true;
};
const ab = { a: "1", b: "2" };
const ba = { b: "2", a: "1" };
assertEqual(eqObjects(ab, ba),true);
const abc = { a: "1", b: "2", c: "3" };
assertEqual(eqObjects(ab, abc),false);
const cd = { c: "1", d: ["2", 3] };
const dc = { d: ["2", 3], c: "1" };
assertEqual(eqObjects(cd, dc),true);
const cd2 = { c: "1", d: ["2", 3, 4] };
assertEqual(eqObjects(cd, cd2),false);