hydrococcus
hydrococcus

Reputation: 487

Angular 2 Service filter parameter is Array

I create a service to get filtered results from a JSON

getItem(id: string): Observable<any[]> {
    return this.http.get('path/to/my.json')
        .map((response) => {
                let results: any = response.json();

                let filtered: any = results.filter((result) => {
                    return result.field === id;
                })[0];

                return filtered;
            }
        );
}

Now my Parameter (id) is not a string rather an array. How must i modify my service? Currently, if 'id' is an array, it returns an Array with all Objects from my JSON.

Upvotes: 0

Views: 710

Answers (1)

pdjota
pdjota

Reputation: 3243

So, assuming it is an array of strings and field should be included in the list you could check that the field is in the list.

ids: string[];

results.filter( (result) => {
    return ids.indexOf(result.field) > -1;
})

Upvotes: 1

Related Questions