How to modify value of an object from an array of properties in JavaScript

Viewed 20

I have an object that looks like this:

const object = {
    "a": {
        "b": {
            "c": "something"
        }
    }
};

I have the names of the properties in an array like so:

const properties = ["a", "b", "c"];

and I want to modify the value of "c". The problem is that the "properties" array is of an unknown length, so I can't just simply do:

object [properties[0]] [properties[1]] [properties[2]];

I managed to access to the value of "c" like this:

let temp = object;
for (let i = 0; i < properties.length; i++) temp = temp[properties[i]];

But with this approach I can only modify the value of "temp". Any ideas?

0 Answers
Related