Let's say I have a string like 'user.data'
, and I want to create the data
field of this object:
const obj = {
user: {
data: {}
}
}
I am not able to do this normally (obj['user.data'] = {}
) with this string because it will do this:
const obj = {
user: {},
'user.data': {}
}
And that is not what I am looking for.
How would I go about creating a property with an object when that is the last part of the string?
const str = 'user.hobbies';
const obj = { user: {} };
addInNestedProp(str, obj);
console.log(obj);
// => { user: { hobbies: {} } }