Skip to content
Advertisement

update nested object with (ES6) computed property name

In my case, I’m using React.js and I would like to dynamically update the values in the deployOptions object.

For example –

initial state looks like:

JavaScript

Obviously this is not correct – but this is what I’m trying to achieve

JavaScript

so that my state would then be

JavaScript

Advertisement

Answer

It’s not especially pretty, but I think this is the best you can do with ES6:

JavaScript

It’s basically equivalent to your own Object.assign solution but using the ES6 spread (...) operator (and argument destructuring for good measure).

Here’s a second option that isn’t as clever but feels a little cleaner to me:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement