You are asking how to modify val
in type1
, so I am assuming there is a fixed path you want to access in your nested objects.
The easiest way is to loop through your objectN
and use an hardcoded path:
for (const key of Object.keys(myList)) {
myList[key].type1.val = newVal;
}
If you want a more dynamic solution to use to explore different paths in your nested object you could use array.prototype.reduce
in a similar way:
function fromPathToObj(path, object) {
// path = "type1.val"
// path is splitted in valid keys, and they are used to access the values with reduce
return path.split(".").reduce((acc, key) => acc[key],object);
}
The previous snippet would become
for (const key of Object.keys(myList)) {
fromPathToObj("type1.val", myList[key]) = newVal;
}
Note: I'm not sure this second solution will modify the original object or you create deep copies of objectN
with fromPathToObj
, but I think it's worth trying.