I have an Observable array and I want to filter/find the Project by name. When I try to use the filter option it is saying
ProjectService.ts
In your Service you can define type or Project[] type to return the response value and same could be continue with filter. e.g. or
and also update your class as suggested by @Sajeetharan
ProjectService.ts
import { Injectable } from '@angular/core';
import { Project } from "../classes/project";
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import { Http } from '@angular/http';
@Injectable()
export class ProjectService {
private projects: Observable;
constructor(private http: Http) {
this.loadFromServer();
}
getProjects(): Observable {
return this.projects;
}
private loadFromServer(): Observable {
this.projects = this.http.get('/api/projects').map((res: Response)=> res.json());
}
getProjectByName(name: string) {
return this.projects.filter(proj => proj.name === name);
}
}
*always write your filters, conditions or manipulation in component not in services.