javascript - Getting a unique array from a service in Angular -
this question has answer here:
i have service contains information members. 1 of variables here team string.
in component, call service method put information in variable before showing in component.
i fill variable "teams" in component unique list of teams in service. should take account writing (so use touppercase()).
my service:
import { injectable } '@angular/core'; @injectable() export class userinfoservice { //methods or services getmembers() { return [ { imageurl: "../assets/images/filler.jpg", firstname: "filler", lastname: "filler", team: "team a", number: "+123456798", bio: "lorem ipsum dolor sit amet,...", isactive: true }, { imageurl: "../assets/images/filler.jpg", firstname: "filler", lastname: "filler", team: "team a", number: "+123456798", bio: "lorem ipsum dolor sit amet,...", isactive: false }, { imageurl: "../assets/images/filler.jpg", firstname: "filler", lastname: "filler", team: "team b", number: "+123456798", bio: "lorem ipsum dolor sit amet,...", isactive: true }, ]; } //constructor constructor() { } }
my component:
import { component, oninit } '@angular/core'; import { userinfoservice } '../services/userinfo.service'; @component({ selector: 'app-teammembers', templateurl: './teammembers.component.html', styleurls: ['./teammembers.component.css'] }) export class teammemberscomponent implements oninit { //props teammembers: any[]; teams:any[]; constructor(userinfoservice: userinfoservice) { //getdata this.teammembers = userinfoservice.getmembers(); } ngoninit() { } }
with es6, can achieve in 1 line. below example:
this.teams = this.teammembers .map(item => item.team) .filter((value, index, self) => self.indexof(value) === index)
Comments
Post a Comment