I ask for help with solving this problem. I only manage to deal with special cases. Input: any JSON object; prototype object. Output: projected object. Projected object structure shall be intersection of source object and prototype object structures. Values of properties in projected object shall be the same as values of respective properties in source object. For example:
const src = {
prop11: {
prop21: 21,
prop22: {
prop31: 31,
prop32: 32
}
},
prop12: 12
};
const proto = {
prop11: {
prop22: null
}
};
This part I was able to do:
const res = (src, proto) => {
return Object.keys(proto).reduce((a, e) => ({
...a, [e]: src[e] ? (proto[e] ? res(src[e], proto[e]) : src[e]) : src[e]
}), {});
}
console.log(res(src, proto));
We get the following result:
{
"prop11": {
"prop22": {
"prop31": 31,
"prop32": 32
}
}
}
But if the proto is different, for example:
const proto = {
prop11: {prop21: 1, prop23: null}
};
This solution gives the wrong result. Spent several days on this task already. I would be grateful for any help!