# 2700. Differences Between Two Objects
###### tags: `leetcode 30 days js challenge` `Medium`
[2700. Differences Between Two Objects](https://leetcode.com/problems/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 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` <= 10<sup>4</sup>
- 2 <= `JSON.stringify(obj2).length` <= 10<sup>4</sup>
### 解答
#### TypeScript
```typescript=
function objDiff(obj1: any, obj2: any): any {
const diff: Record<string, any> = {};
if (Array.isArray(obj1) && Array.isArray(obj2)) {
for (let i = 0; i < Math.max(obj1.length, obj2.length); i++) {
const val1 = obj1[i];
const val2 = obj2[i];
if (val1 !== undefined && val2 !== undefined) {
if (
(isObjectLiteral(val1) && isObjectLiteral(val2)) ||
(Array.isArray(val1) && Array.isArray(val2))
) {
const nestedDiff = objDiff(val1, val2);
if (Object.keys(nestedDiff).length > 0) {
diff[i] = nestedDiff;
}
} else if (val1 !== val2) {
diff[i] = [val1, val2];
}
}
}
} else {
for (const key of Object.keys(obj1)) {
if (key in obj2) {
if (
(isObjectLiteral(obj1[key]) && isObjectLiteral(obj2[key])) ||
(Array.isArray(obj1[key]) && Array.isArray(obj2[key]))
) {
const nestedDiff = objDiff(obj1[key], obj2[key]);
if (Object.keys(nestedDiff).length > 0) {
diff[key] = nestedDiff;
}
} else if (obj1[key] !== obj2[key]) {
diff[key] = [obj1[key], obj2[key]];
}
}
}
}
return diff;
}
const isObjectLiteral = (obj: any) => {
return typeof obj === 'object' && obj !== null && !Array.isArray(obj);
};
```
> [name=Sheep][time=Wed, May 24, 2023]
解法二:
```typescript=
function objDiff(obj1: any, obj2: any): any {
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 diff: Record<string, any> = {};
for (const key of Object.keys(obj1)) {
if (key in obj2) {
const nestedDiff = objDiff(obj1[key], obj2[key]);
if (Object.keys(nestedDiff).length > 0) {
diff[key] = nestedDiff;
}
}
}
return diff;
}
```
> [name=Sheep][time=Wed, May 24, 2023]
### Reference
[回到題目列表](https://hackmd.io/@Marsgoat/leetcode_every_day)