Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

178
Views
Filtro angular Matriz observable

Tengo una matriz Observable y quiero filtrar/encontrar el Proyecto por nombre. Cuando trato de usar la opción de filtro, dice

ingrese la descripción de la imagen aquí

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<Project[]>; constructor(private http: Http) { this.loadFromServer(); } getProjects(): Observable<Project[]> { return this.projects; } private loadFromServer() { this.projects = this.http.get('/api/projects').map(res => res.json()); } getProjectByName(name: String) { return this.projects.filter(proj => proj.name === name); } }

Clase de proyecto

 export class Project { public name: String; public miniDesc: String; public description: String; public category: String[]; public images: any[]; }
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

debería ser:

 getProjectByName(name: String) { return this.projects .map(projects => projects.filter(proj => proj.name === name)); }

entendiste mal sobre el operador de filtro. El operador que se usa para filtrar los datos devueltos de la secuencia. Su flujo devuelve una matriz de objetos, por lo que necesita una filter array para obtener el valor que necesita.

La solución anterior devolverá una matriz después de filtrada, si desea obtener solo un valor, use la siguiente solución

 getProjectByName(name: String) { return this.projects .map(projects => { let fl = projects.filter(proj => proj.name === name); return (fl.length > 0) ? fl[0] : null; }); }
about 4 years ago · Santiago Trujillo Report

0

En su Servicio, puede definir el tipo <any> o el tipo Project[] para devolver el valor de respuesta y lo mismo podría continuar con el filtro. por ejemplo <any>res.json() o <Project[]>res.json()

y también actualice su clase como lo sugiere @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<Project[]>; constructor(private http: Http) { this.loadFromServer(); } getProjects(): Observable<Project[]> { return this.projects; } private loadFromServer(): Observable<any> { this.projects = this.http.get('/api/projects').map((res: Response)=> <any>res.json()); } getProjectByName(name: string) { return this.projects.filter(proj => proj.name === name); } }

*siempre escribe tus filtros, condiciones o manipulación en componente no en servicios.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!