After thorough checking, I have confirmed that the property does exist with the correct key. However, it is returning an error message stating name is not a property of {}
. I attempted to assign this object to an interface along with its properties but encountered another issue where it wouldn't allow me to do so with an empty object {}. What could possibly be causing this problem?
The following code snippet is the one causing the issue:
this.companiesArray[0].employees[0].name
which triggers the error name is not a property of {}
.
Here is an example of the data structure I am working with, represented as an array of objects (Companies)
[
{
"employees": [
{
"name": "Amy",
"gender": "female",
"level": "2"
},
{
"name": "Chris",
"gender": "male",
"level": "4"
}
],
"company": "XYZ",
"tower": "Tower 2"
},
{
"employees": [
{
"name": "Ben",
"gender": "male",
"level": "1"
},
{
"name": "Sarah",
"gender": "female",
"level": "1"
}
],
"company": "ABC",
"tower": "Tower 3"
}
]
Below is the Angular code and the Company Interface being utilized
companiesArray: Company[]
ngOnInit() {
this.companyService.obsCompanies.subscribe(companies => {
this.companiesArray = companies;
console.log(this.companiesArray[0].employees[0].name);// for testing
});
...
}
export interface Company {
employees: {}[],
company: string,
tower: string
}