# 2755. Deep Merge of Two Objects

## Description

Given two values obj1 and obj2, return a deepmerged value.

Values should be deepmerged according to these rules:

• If the two values are objects, the resulting object should have all the keys that exist on either object. If a key belongs to both objects, deepmerge the two associated values. Otherwise, add the key-value pair to the resulting object.
• If the two values are arrays, the resulting array should be the same length as the longer array. Apply the same logic as you would with objects, but treat the indices as keys.
• Otherwise the resulting value is obj2.

You can assume obj1 and obj2 are the output of JSON.parse().

Example 1:

Input: obj1 = {"a": 1, "c": 3}, obj2 = {"a": 2, "b": 2}
Output: {"a": 2, "c": 3, "b": 2}
Explanation: The value of obj1["a"] changed to 2 because if both objects have the same key and their value is not an array or object then we change the obj1 value to the obj2 value. Key "b" with value was added to obj1 as it doesn't exist in obj1.


Example 2:

Input: obj1 = [{}, 2, 3], obj2 = [[], 5]
Output: [[], 5, 3]
Explanation: result = obj2 because obj1 and obj2 have different types. result = obj1 because obj2 does not exist.


Example 3:

Input:
obj1 = {"a": 1, "b": {"c": [1 , [2, 7], 5], "d": 2}},
obj2 = {"a": 1, "b": {"c": [6, , ], "e": 3}}
Output: {"a": 1, "b": {"c": [6, [6, 7], ], "d": 2, "e": 3}}
Explanation:
Arrays obj1["b"]["c"] and obj2["b"]["c"] have been merged in way that obj2 values overwrite obj1 values deeply only if they are not arrays or objects.
obj2["b"]["c"] has key "e" that obj1 doesn't have so it's added to obj1.


Example 4:

Input: obj1 = true, obj2 = null
Output: null


Constraints:

• 1 <= JSON.stringify(obj1).length <= 5 * 105
• 1 <= JSON.stringify(obj2).length <= 5 * 105

## Solutions

• function deepMerge(obj1: any, obj2: any): any {
const isObj = (obj: any) => obj && typeof obj === 'object';
const isArr = (obj: any) => Array.isArray(obj);
if (!isObj(obj1) || !isObj(obj2)) {
return obj2;
}
if (isArr(obj1) !== isArr(obj2)) {
return obj2;
}
for (const key in obj2) {
obj1[key] = deepMerge(obj1[key], obj2[key]);
}
return obj1;
}

/**
* let obj1 = {"a": 1, "c": 3}, obj2 = {"a": 2, "b": 2};
* deepMerge(obj1, obj2); // {"a": 2, "c": 3, "b": 2}
*/
{"ft_lang_label":"__label__en","ft_lang_prob":0.7378379,"math_prob":0.9762567,"size":2313,"snap":"2023-40-2023-50","text_gpt3_token_len":769,"char_repetition_ratio":0.13209182,"word_repetition_ratio":0.06779661,"special_character_ratio":0.3916991,"punctuation_ratio":0.2338403,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9706886,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-11-28T18:55:49Z\",\"WARC-Record-ID\":\"<urn:uuid:a2fcde4d-cb7b-44ae-986c-43b0658ab783>\",\"Content-Length\":\"26019\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:d2a8cd33-197d-41af-a07d-e2c79038912c>\",\"WARC-Concurrent-To\":\"<urn:uuid:461b7f48-c3d0-4882-9487-a68c3bdf1b69>\",\"WARC-IP-Address\":\"18.165.83.109\",\"WARC-Target-URI\":\"https://leetcode.ca/2023-10-09-2755-Deep-Merge-of-Two-Objects/\",\"WARC-Payload-Digest\":\"sha1:GXGAVAC35MFJ5UXRMHV6HE75GR4AWVAW\",\"WARC-Block-Digest\":\"sha1:AA5RDK3JWNXAVLLD64Q6MTDI3L52FVSH\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679099942.90_warc_CC-MAIN-20231128183116-20231128213116-00508.warc.gz\"}"}