Skip to content
Advertisement

Updating nested object by path in javascript

Let’s say I have this object:

JavaScript

I’d like to be able to update the price in this object through a function call as follows:

JavaScript

This answer Javascript: how to dynamically create nested objects using object names given by an array is good but does not address the case when there are arrays in the object.

Underscore acceptable.

Note: this answer Javascript: how to dynamically create nested objects INCLUDING ARRAYS using object names given by an array does not cut it because I don’t have array references in that form (products[1])

Advertisement

Answer

You need to slightly modify function from the linked answer

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