angular - How to filter elements of array based on id in Ionic2? -
i developing mobile app using ionic2. intent dynamically insert people in homepage. whenever click person, list of dynamically added items correlated person (with id) should show up. not able filter items each person, how can filter items specific person id?
this home add new people entries:
public people:{personname:string, personid:string}[] = []; //will load people in array ionviewwillenter(){ //addpersons constructed addpersonsservice this.people = this.addpersons.getpeople(); } //from html, parse personid array //it redirect list of items specific person viewdebts(personid:string){ this.navctrl.push(debtslistpage, personid); }
this addpersonsservice
public people: {personname:string, personid:string}[] = []; //this called upon click html addperson(person:{personname:string, personid:string} ){ this.people.push(person); } getpeople(){ return this.people.slice(); } generateid(id:string){ return id; }
in here displaying items each person
debtvalue:{persondebt:string, personid:string}[] = []; //this list of items displayed ionviewwillenter(){ var id = this.navparams; //getting id previous page this.debtvalue = this.debts.getpersonsdebt(id); //debts comes displaydebtsservice } //adding item adddebts(debt:{persondebt:string, personid:string}) { this.debts.adddebt(debt); }
and displaydebtsservice
public debt:{persondebt:string, personid:string}[] = []; adddebt(debt:{persondebt:string, personid:string}){ this.debt.push(debt); } getpersonsdebt(id:any){ //this struggling. how filter results based on id of person? return this.debt; }
thanks help
have try .find()
? .. or .filter()
..
but here think find() better cause you're filter id ..which looks pk key .. aspect 1 person returning method , not more (like filter() )
something like:
getpersonsdebt(id:any){ //this struggling. how filter results based on id of person? return this.debt.find(p=> p.id === id); }
hope helps you
Comments
Post a Comment