-
Notifications
You must be signed in to change notification settings - Fork 0
/
eqObjects.js
82 lines (70 loc) · 2.09 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const assertEqual = function(actual, expected) {
if (actual === expected) {
console.log(`✅ Assertion Passed: ${actual} === ${expected}`);
} else {
console.log(`❌ Assertion Failed: ${actual} !== ${expected}`);
}
};
const eqArrays = function(arr1, arr2) {
if (arr1.length !== arr2.length) {
return false;
} else {
for (let i = 0; i < arr1.length; i++) {
if (arr1[i] === arr2 [i]) {
continue;
} else {
return false;
}
}
} return true;
};
const eqObjects = function(object1, object2) {
if (Object.keys(object1).length !== Object.keys(object2).length) {
return false;
} else {
for (let i of Object.keys(object1)) {
if (Array.isArray(object2[i]) && Array.isArray(object1[i])) {
if (!eqArrays(object2[i], object1[i])) {
return false;
} else {
if (!Array.isArray(object2[i]) && !Array.isArray(object1[i])) {
if ((object2[i]) !== object1[i]) {
return false;
}
}
} return true;
} else if (object2[i] !== object1[i]) {
return false;
}
}
} return true;
};
// const eqObjects = function(object1, object2) {
// if (Object.keys(object1).length !== Object.keys(object2).length){
// return false;
// } else {
// for(let i of Object.keys(object1)){
// if (object2[i] !== object1[i]){
// return false
// }
// }
// } return true;
// }
// let ab = {a: "1", b: "2"};
// let ba = {b: "2", a: "1" };
// let abc = {a :"1", b: "2", c:"3"};
// let def = {a :"1", b: "2", c:"3"};
// assertEqual(eqObjects(ab,ba), true);
// assertEqual(eqObjects(ab, abc), true);
// assertEqual(eqObjects(abc, def), true);
// let cd = {c: "1", d:["2", 3]};
// let dc = {d: ["2", 3], c: "1"};
// assertEqual(eqObjects(cd, dc), true);
// let ef = {c: "1", d:["2", 3]};
// let gh = {d: ["2", 3], c: "2"};
// assertEqual(eqObjects(ef, gh), true);
// let cd2 = {c: "1", d: ["2", 3 ,4]};
// assertEqual(eqObjects(cd, cd2), true);
// //console.log(eqArrays(cd["d"], cd2["d"]));
// console.log(eqObjects(cd, dc))
module.exports = eqObjects;