I was trying to map nested object, and I got this error
'XYZ' property doesn't exist on the type 'ABC'
I was accessing the properties using dot notation in get-lodash function. It didn't work so I got to know another approach to access properties. It is square brackets notation.
Let me elaborate using example
This is the Object
let company = {
name: "Amazon",
contact: {
phone: "88076281093",
email: "amazon123@xyz.com",
address: {
city: "Pune",
country: "India"
}
}
};
Dot notation to access it :
console.log(company.contact.email);
console.log(company.contact.address.city);`
Square Bracket notation to access it :
console.log(person["contact"]["email"]);
console.log(person["contact"]["address"]["country"]);
OR
console.log(person["contact","email"]);
console.log(person["contact","address","country"]);
Also in #typescript when you define type for nested object you can do it in this way
let company = {
name: string,
phone: number,
email: string,
city: string,
country: string
}
Hope it helps if you are struggling..
Reference link:https://www.geeksforgeeks.org/how-to-access-and-process-nested-objects-arrays-or-json/
Top comments (1)
Informative