Coming off my previous question (which was about getting the property), I need to set the value of a nested object member using a dynamic property name.
const settings = {
service: {
username: 'foo',
password: 'bar'
}
};
const settingName = 'service.username';
const newValue = 'baz';
settings[settingName] = newValue ; // doesn't work
console.log(settings.service.username); // foo
The only way I can think of is to use eval:
eval(`settings.${settingName} = "${newValue}"`);
const settings = {
service: {
username: 'foo',
password: 'bar'
}
}
const settingName = 'service.username';
const newValue = 'baz';
eval(`settings.${settingName} = "${newValue}"`); // works, but bad
console.log(settings.service.username);
But this has problems (for example, the example above assumes the new value is a string). Is there a way to assign a property of a nested object whose name is not known without using eval?