Angular 2 – get the right object from an array of objects

I got a list of Peoples (people object) from my json API and I saved it in a variable accessed via this.people.

I need to access the person where his pivot object age = 15 assuming there is only one pivot object defind with age of 15.

  1. Can I access this specific object with angular 2?
  2. If not, will I have to change my API to make a new request for it?

Thanks in advance!

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

You could implement a custom pipe: @Pipe({ name: 'filter' }) export class FilterPipe { transform(val) { return val.filter((elt) => elt.age === 15); } } and use it this way: {{people | filter}} Don’t forget to add your pipe into the pipes attribute of the component where you want to use it: @Component({ (...) pipes: [ FilterPipe ] }) You could make this pipe a bit generic with parameters: @Pipe({ name: 'filter' }) export class FilterPipe { transform(val, params) { var field = params[0]; var fieldValue = params[1]; return val.filter((elt) => elt[field] === fieldValue); } } and use it this way:… Read more »