/
2700. Differences Between Two Objects.js
135 lines (119 loc) · 3.28 KB
/
2700. Differences Between Two Objects.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
Write a function that accepts two deeply nested objects or arrays obj1 and obj2 and returns a new object representing their differences.
The function should compare the properties of the two objects and identify any changes. The returned object should only contains keys where the value is different from obj1 to obj2. For each changed key, the value should be represented as an array [obj1 value, obj2 value]. Keys that exist in one object but not in the other should not be included in the returned object. When comparing two arrays, the indices of the arrays are considered to be their keys. The end result should be a deeply nested object where each leaf value is a difference array.
You may assume that both objects are the output of JSON.parse.
Example 1:
Input:
obj1 = {}
obj2 = {
"a": 1,
"b": 2
}
Output: {}
Explanation: There were no modifications made to obj1. New keys "a" and "b" appear in obj2, but keys that are added or removed should be ignored.
Example 2:
Input:
obj1 = {
"a": 1,
"v": 3,
"x": [],
"z": {
"a": null
}
}
obj2 = {
"a": 2,
"v": 4,
"x": [],
"z": {
"a": 2
}
}
Output:
{
"a": [1, 2],
"v": [3, 4],
"z": {
"a": [null, 2]
}
}
Explanation: The keys "a", "v", and "z" all had changes applied. "a" was chnaged from 1 to 2. "v" was changed from 3 to 4. "z" had a change applied to a child object. "z.a" was changed from null to 2.
Example 3:
Input:
obj1 = {
"a": 5,
"v": 6,
"z": [1, 2, 4, [2, 5, 7]]
}
obj2 = {
"a": 5,
"v": 7,
"z": [1, 2, 3, [1]]
}
Output:
{
"v": [6, 7],
"z": {
"2": [4, 3],
"3": {
"0": [2, 1]
}
}
}
Explanation: In obj1 and obj2, the keys "v" and "z" have different assigned values. "a" is ignored because the value is unchanged. In the key "z", there is a nested array. Arrays are treated like objects where the indices are keys. There were two alterations to the the array: z[2] and z[3][0]. z[0] and z[1] were unchanged and thus not included. z[3][1] and z[3][2] were removed and thus not included.
Example 4:
Input:
obj1 = {
"a": {"b": 1},
}
obj2 = {
"a": [5],
}
Output:
{
"a": [{"b": 1}, [5]]
}
Explanation: The key "a" exists in both objects. Since the two associated values have different types, they are placed in the difference array.
Example 5:
Input:
obj1 = {
"a": [1, 2, {}],
"b": false
}
obj2 = {
"b": false,
"a": [1, 2, {}]
}
Output:
{}
Explanation: Apart from a different ordering of keys, the two objects are identical so an empty object is returned.
Constraints:
2 <= JSON.stringify(obj1).length <= 104
2 <= JSON.stringify(obj2).length <= 104
*/
/**
* @param {object} obj1
* @param {object} obj2
* @return {object}
*/
function objDiff(obj1, obj2, diff = {}) {
for (let key in obj1) {
let bothArrays = Array.isArray(obj1[key]) && Array.isArray(obj2[key]);
let bothObjects = typeof obj1[key] === 'object' &&
typeof obj2[key] === 'object' &&
!Array.isArray(obj1[key]) &&
!Array.isArray(obj2[key]);
if (bothObjects || bothArrays) {
diff[key] = diff[key] ?? {};
objDiff(obj1[key], obj2[key], diff[key]);
if (Object.keys(diff[key]).length === 0) {
delete diff[key];
}
} else {
if (obj1[key] !== obj2[key] && key in obj2) {
diff[key] = [obj1[key], obj2[key]]
}
}
}
return diff;
};