Struggling to find a solution for accessing nested object properties dynamically?
The property path needs to be represented as an array of strings.
For example, to retrieve the label, use ['type', 'label']
I'm at a roadblock with this issue, any suggestions?
Edit snippet:
Demo
var parent = {
type: {
id: "2",
label: "3",
}
};
function getNestedLabel(ids){
if (ids.length === 1) {
return parent[ids[0]];
}
var result = parent;
for (let i = 0; i < ids.length; i++) {
result = result[ids[i]];
}
return result;
}
console.log(getNestedLabel(["type", "label"]));