Sometimes, we want to dynamically set property of nested object with JavaScript.
In this article, we’ll look at how to dynamically set property of nested object with JavaScript.
How to dynamically set property of nested object with JavaScript?
To dynamically set property of nested object with JavaScript, we can use the Lodash set method.
For instance, we write
_.set(obj, "foo.bar", "baz");
to call set with obj, 'foo.bar' and 'baz' to set obj.foo.bar to 'baz'.
The property will be created if it doesn’t exist.
Conclusion
To dynamically set property of nested object with JavaScript, we can use the Lodash set method.