-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2700-differences-between-two-objects.js
More file actions
50 lines (45 loc) · 1.55 KB
/
2700-differences-between-two-objects.js
File metadata and controls
50 lines (45 loc) · 1.55 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
/**
* 2700. Differences Between Two Objects
* https://leetcode.com/problems/differences-between-two-objects/
* Difficulty: Medium
*
* 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. The end result should be a deeply nested object where each leaf value is a
* difference array.
*
* When comparing two arrays, the indices of the arrays are considered to be their keys.
*
* You may assume that both objects are the output of JSON.parse.
*/
/**
* @param {Object|Array} obj1
* @param {Object|Array} obj2
* @return {Object|Array}
*/
function objDiff(obj1, obj2) {
if (obj1 === obj2) return {};
if (obj1 === null || obj2 === null || typeof obj1 !== typeof obj2
|| typeof obj1 !== 'object') {
return [obj1, obj2];
}
if (Array.isArray(obj1) !== Array.isArray(obj2)) {
return [obj1, obj2];
}
const result = {};
for (const key in obj1) {
if (key in obj2) {
const difference = objDiff(obj1[key], obj2[key]);
if (Object.keys(difference).length > 0 || Array.isArray(difference)) {
result[key] = difference;
}
}
}
return result;
}