I'm trying to search inside an object array to see if any object contains the object I'm looking for and assign it to a variable.
This is the interface that I'm using. Basically I have an array of countries, each of which has it's own array of cities.
import { ICity } from "./city";
export interface ICountry {
name: string,
capital: string,
language: string,
population: number,
density: number,
area: number,
majorCities: ICity[]
}
The object I'm looking for is the city parameter of this function, but it always returns undefined. What is the best way to find country which a certain city belongs to?
remove(city: ICity): void {
var country;
this.countries.forEach(cn => {
if (cn.majorCities.includes(city)) {
country = cn;
console.log(cn);
}
});
console.log(country);
}