I have the following JavaScript / Typescript object:
data = {
customer: {
id: 1,
name: "X",
address: {
line1: "address line 1",
line2: "address line 2",
country: {
code: "US",
name: "United States"
}
}
}
};
I would like to dynamically access specific property value.
Example: data.customer.address.country.code should return "US".
I am able to dynamically set the last property in the object hierarchy.
console.log(data.customer.address.country["code"]) // This works
Is there anyway I access any property in the object dynamically?
I tried:
console.log(data["customer.address.country.code"]) // Does not work
console.log(data["customer.id"]) // Does not work
Code Snippet:
data = {
customer: {
id: 1,
name: "X",
address: {
line1: "address line 1",
line2: "address line 2",
country: {
code: "US",
name: "United States"
}
}
}
};
console.log(data.customer.address.country["code"]) // This works
console.log(data["customer.address.country.code"]) // Does not work
console.log(data["customer.id"]) // Does not work