I have a unique dataset in JSON format that includes information about countries and states. For example:
{
"countries": [
{
"id": 1,
"name": "United States"
},
{
"id": 2,
"name": "India"
}],
"states": [
{
"id": 1,
"countryId": 1,
"name": "Alabama"
},
{
"id": 2,
"countryId": 1,
"name": "Alaska"
} ] }
With this data, I created a Service to retrieve the countries and states for displaying in dropdown menus:
export class CountriesService {
constructor(private http: HttpClient) { }
public getCountries(): Observable<Country[]> {
return this.http.get<Country[]>("assets/data.json").pipe(map(obj => obj["countries"]));
}
public getStates(countryId: number): Observable<State[]> {
return this.http.get<State[]>("assets/data.json").pipe(
map(res => res["states"]),
map(res => { if (res.countryId === countryId) return res;}));
}
}
Although the getCountries()
method works perfectly, fetching all the countries, I am facing issues retrieving specific states based on the countryId
using the getStates
method.
Currently, this method is not returning any results. Any idea what might be going wrong?