I am currently attempting to search through an object array in order to find any objects that contain the specific object I am seeking. Once found, I want to assign it to a variable.
The interface being used is for an array of countries, each containing its 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 city parameter in this function is what I am searching for, but it always returns undefined. What would be the most effective method to locate the country to which a given city belongs?
remove(city: ICity): void {
var country;
this.countries.forEach(cn => {
if (cn.majorCities.includes(city)) {
country = cn;
console.log(cn);
}
});
console.log(country);
}