-
Notifications
You must be signed in to change notification settings - Fork 0
/
eqObjects.js
46 lines (40 loc) · 1.22 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
const assertEqual = function(actual, expected) {
if (actual === expected) {
console.log(`The '${actual}' and the '${expected}' are the same✅`);
} else console.assert(actual === expected, "❌");
};
const eqObjects = function(object1, object2) {
const keys1 = Object.keys(object1)
const keys2 = Object.keys(object2)
if(keys1.length = keys2.length){
for(key of keys1){
if(Array.isArray(object1[key]) || Array.isArray(object2[key])) {
if(object1[key].length === object2[key].length){
eqArrays(object1[key], object2[key]);
} else {
return false;
}
} else{
if(object1[key] === object2[key]){
} else return false;
}
}
} else return false;
return true;
}
const eqArrays = function (array1, array2) {
for (let i = 0; i < array1.length; i++ ) {
if (array1[i] !== array2[i]) {
return false;
}
}
return true;
};
const ab = { a: "1", b: "2" };
const ba = { b: "2", a: "1" };
assertEqual((eqObjects(ab, ba)), true);
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);