Good day.
There are two some unknown objects. They need to be combined into one. What should be connected according to the rules:
- if in the object
a
and in the objectb
there is the same object (or a subobject of ANY nesting), then leave the object fromb
; - if in object
a
and in objectb
there is the same object and it is an array , then both of these arrays must be joined; - if in object
b
there is an object missing ina
, then add it. - You must either change
a
, or get a new object.
I will give an example:
a: { car: "WW", detail:{ wheel: { left: "good", right:"bad" } }, props: [ {prop1: "1"}, {prop1: "2"}, {prop1: "3"} ] }; // Теперь объект b, который надо совместить: b: { detail:{ wheel: { left: "bad",//изменяем свойство из a на это center: "good"//появилось новое свойство } } props: [ {prop1: "4"}//Добавляем в массив к a. ] } //вот что получиться должно: c: { car: "WW", detail:{ wheel: { left: "bad", right:"bad", center: "good" } }, props: [ {prop1: "1"}, {prop1: "2"}, {prop1: "3"}, {prop1: "4"} ] };
PS :
Without using any libraries.