Skip to content

2755. Deep Merge of Two Objects

 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
type JSONValue =
  | null
  | boolean
  | number
  | string
  | JSONValue[]
  | { [key: string]: JSONValue };

function deepMerge(obj1: JSONValue, obj2: JSONValue): JSONValue {
  if (typeof obj1 !== 'object' || typeof obj2 !== 'object') {
    return obj2;
  }
  if (obj1 === null || obj2 === null) {
    return obj2;
  }
  if (Array.isArray(obj1) !== Array.isArray(obj2)) {
    return obj2;
  }
  const ans = obj1;
  for (const key in obj2) {
    if (key in ans) {
      ans[key] = deepMerge(ans[key], obj2[key]);
    } else {
      ans[key] = obj2[key];
    }
  }
  return ans;
}