To combine objects with updated values?

Hello! Don't tell me how to combine 2 objects, what would the value updated and added, not perestaralis, as in the example with the key "b" => "b2". Prompt how better to solve this problem?
obj1 = {a:1, b: {b1: 1, b2: 4}}
obj2 = {b: {b1: 2, b3: 3}, c:1 }
console.log(Object.assign(obj1, obj2));
// what is --- { a: 1, b: { b1: 2, b3: 3 }, c: 1 }
// what you need ---{ a: 1, b: { b1: 2, b2:4, b3: 3 }, c: 1 }
June 26th 19 at 14:23
2 answers
June 26th 19 at 14:25
June 26th 19 at 14:27
Thanks for the replies here's what I managed to create
isObject = (item) ->
 item and typeof item is 'object' and not Array.isArray item


mergeDeep = (target, source) ->

 if isObject(target) and isObject(source)
 Object.keys(source).forEach (key) ->
 if isObject source[ key ]
 if not target[key] then Object.assign target { "#{key}": {} }
 mergeDeep target[ key ], source[ key ]
else
 Object.assign target { "#{key}": source[key] }
target


obj1 = {a:1, b: {b1: 1, b2: 4}}
obj2 = {b: {b1: 2, b3: {b11:4, b21:3}}, c:{c1: {c12: 5} } }
console.log(mergeDeep(obj1, obj2));

###
{ a: 1,
 b: { b1: 2, b2: 4, b3: { b11: 4, b21: 3 } },
 c: { c1: { c12: 5 } } }
###

Find more questions by tags JavaScript