-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2700-differences-between-two-objects.js
More file actions
41 lines (36 loc) · 1.04 KB
/
Copy path2700-differences-between-two-objects.js
File metadata and controls
41 lines (36 loc) · 1.04 KB
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
/**
* @param {object} obj1
* @param {object} obj2
* @return {object}
*/
function objDiff(obj1, obj2) {
// If both are primitives(not objects), return empty if they equal else show difference
if (!isObject(obj1) && !isObject(obj2)) {
return obj1 === obj2 ? {} : [obj1, obj2];
}
// If one is primitive and other is object
if (!isObject(obj1) || !isObject(obj2)) {
return [obj1, obj2];
}
// If one is array and the other is object
if (Array.isArray(obj1) !== Array.isArray(obj2)) {
return [obj1, obj2];
}
// If both array or both object, then recursion
const diff = {};
for (const key in obj1) {
// look for obj1 keys present in obj2 (common keys)
if (obj2.hasOwnProperty(key)) {
const result = objDiff(obj1[key], obj2[key]);
// console.log('result: ' + result);
// check if result object we get is empty or not
if (Object.keys(result).length > 0) {
diff[key] = result;
}
}
}
return diff;
function isObject(obj) {
return typeof obj === 'object' && obj !== null;
}
}