The Scenario
There is a class named myClass
:
export class myClass
{
name: string;
age: number;
city: string;
}
and another class called people
:
export class people
{
name: string;
age: number;
}
In the component.ts
, a variable listMyClass : myClass[];
has been declared and it gets populated with data from an API during ngOnInit()
.
The Objective
The goal is to create a method in component.ts
that will iterate through listMyClass
and add items to a list of the people
class if their city matches "london".
An attempt was made to write a method outside of ngOnInit()
:
getLondonPeople(){
for (let item in listMyClass) {
if (item.city == "london") {
//do something
}
}
return listPeople;
}
The Issue
An error occurs on item.city
with the message:
Property 'city' does not exist on type 'string'.
How can this be resolved?