Categories
JavaScript Answers

How to dynamically set property of nested object with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *