vendredi 2 septembre 2016

How to modify an object property given its location within the object

Given an object obj, I can modify its properties by using something like obj.a.b.c = "new value". However, I want to be able to do this programmatically, with the property's location in the form of an array. How can I make a function that looks like this:

modifyProperty(obj, ["a", "b", "c"], "new value");

and is equivalent to

obj.a.b.c = "new value";

?





Aucun commentaire:

Enregistrer un commentaire