I have this object {country:{town:{company:{boss:{}}}}}
and this string country.town.cityhouse.major
, I need to updatr the object from the string, but keeping the previous properties and data.
{
country: {
town: {
company: {
boss: {}
},
cityhouse: {
major: {}
}
}
}
}
This is what I have so far:
function updateObj(object, path) {
const newPath = path.split('.');
let temp = {...object};
for (let i = 0; i < newPath.length; i++) {
let mid = temp[newPath[i]];
if (mid) {
temp = mid;
} else {
temp[newPath[i]] = {};
}
}
return temp;
}
const obj = { country: { town: { company: { boss: {} }}}};
const r = updateObj(obj, 'country.town.cityhouse.major');
console.log(r);
but it responds:
{
company: {
boss: {}
},
cityhouse: {},
major: {}
}
Any hint on this?