-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2700-DifferencesBetweenTwoObjects.js
More file actions
215 lines (194 loc) · 5.96 KB
/
2700-DifferencesBetweenTwoObjects.js
File metadata and controls
215 lines (194 loc) · 5.96 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// 2700. Differences Between Two Objects
// 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 changed 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:
// obj1 and obj2 are valid JSON objects or arrays
// 2 <= JSON.stringify(obj1).length <= 10^4
// 2 <= JSON.stringify(obj2).length <= 10^4
/**
* @param {Object|Array} obj1
* @param {Object|Array} obj2
* @return {Object|Array}
*/
function objDiff(obj1, obj2) {
// 递归时 相同直接返回 {} 不加入到 res 有 判断 Object.keys(subDiff).length > 0
if (obj1 === obj2) return {};
if (obj1 === null || obj2 === null) return [obj1, obj2];
// 存在一个为对象 一个不为空的时的处理)
if (typeof obj1 !== 'object' || typeof obj2 !== 'object') return [obj1, obj2];
if (Array.isArray(obj1) !== Array.isArray(obj2)) return [obj1, obj2];
const res = {};
for (const key in obj1) { // 循环 obj1
if (key in obj2) { // 如果 obj2 存在相关 key 需要比较相关值
// 递归到值
const subDiff = objDiff(obj1[key], obj2[key]);
// 如果返回的不是空对象 说明存在差值
if (Object.keys(subDiff).length > 0) res[key] = subDiff;
}
}
return res;
};
// best solution
function objDiff1(obj1, obj2) {
if((Array.isArray(obj1) && !Array.isArray(obj2)) ||
(!Array.isArray(obj1) && Array.isArray(obj2))) {
return [obj1, obj2]
}
const res = {};
Object.keys(obj1).forEach(key => {
if(obj2[key] !== undefined) {
if(obj1[key] instanceof Object && obj2[key] instanceof Object) {
let diffRes = objDiff(obj1[key], obj2[key]);
if(Object.keys(diffRes).length > 0) {
res[key] = diffRes;
}
} else {
if(obj1[key] !== obj2[key]) {
res[key] = [obj1[key], obj2[key]];
}
}
}
})
return res;
};
// Example 1:
console.log(objDiff(
{},
{"a": 1,"b": 2}
)); // {}
// Example 2:
console.log(objDiff(
{ "a": 1, "v": 3, "x": [], "z": { "a": null} },
{ "a": 2, "v": 4, "x": [], "z": { "a": 2 } }
)); // {"a": [1, 2], "v": [3, 4],"z": { "a": [null, 2] } }
// Example 3:
console.log(objDiff(
{ "a": 5, "v": 6, "z": [1, 2, 4, [2, 5, 7]]},
{ "a": 5, "v": 7, "z": [1, 2, 3, [1]]}
)); // { "v": [6, 7], "z": {"2": [4, 3], "3": { "0": [2, 1] } } }
// Example 4:
console.log(objDiff(
{ "a": {"b": 1} },
{ "a": [5] }
)); // { "a": [{"b": 1}, [5]] }
// Example 5:
console.log(objDiff(
{ "a": [1, 2, {}], "b": false },
{ "b": false, "a": [1, 2, {}] }
)); // {}
// Example 1:
console.log(objDiff1(
{},
{"a": 1,"b": 2}
)); // {}
// Example 2:
console.log(objDiff1(
{ "a": 1, "v": 3, "x": [], "z": { "a": null} },
{ "a": 2, "v": 4, "x": [], "z": { "a": 2 } }
)); // {"a": [1, 2], "v": [3, 4],"z": { "a": [null, 2] } }
// Example 3:
console.log(objDiff1(
{ "a": 5, "v": 6, "z": [1, 2, 4, [2, 5, 7]]},
{ "a": 5, "v": 7, "z": [1, 2, 3, [1]]}
)); // { "v": [6, 7], "z": {"2": [4, 3], "3": { "0": [2, 1] } } }
// Example 4:
console.log(objDiff1(
{ "a": {"b": 1} },
{ "a": [5] }
)); // { "a": [{"b": 1}, [5]] }
// Example 5:
console.log(objDiff1(
{ "a": [1, 2, {}], "b": false },
{ "b": false, "a": [1, 2, {}] }
)); // {}