Unfortunately, there isn't a predefined function for that specific task.
However, numerous third-party modules can assist in achieving the desired outcome.
One such module is lodash
, which offers the convenient _.set
method. You can access the documentation here. Alternatively, you have the option to utilize the standalone lodash.set
package without including the entire lodash
library.
Here's an example from the official docs:
var object = { 'a': [{ 'b': { 'c': 3 } }] };
_.set(object, 'a[0].b.c', 4);
console.log(object.a[0].b.c);
// => 4
_.set(object, ['x', '0', 'y', 'z'], 5);
console.log(object.x[0].y.z);
// => 5
If _.set
doesn't align with your preference, there are several alternative options available through libraries like dot-prop, set-value, object-path, and dot-object.