I am trying to write a re-usable javascript function to insert key/value pairs into variable path locations within an object. Assume I have an object like this.
const obj ={
firstName: 'Bob',
lastName: 'Smith'
}
I have created a re-usable function like so
function mapData(doc, path) {
const keyNames = Object.keys(doc);
const bigDoc = {};
keyNames.forEach(keyname => {
bigDoc[keyname] = doc[keyname];
});
return bigDoc;
}
Now I want to be able to place it under the correct path under bigDoc, like for example bigDoc.teacher.math or bigDoc.student.yearten by calling the method mapData(obj, 'teacher.math') or mapData(obj, 'student.yearten') so that, for example, bigDoc.teacher.math.firstName is set to 'Bob. I know how to do it with one level deep, but I don't know how for two or more levels deep.
Please help.