Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - How to filter ngFor to specific object property data

Tags:

angular

I have this data in in-memory-data.service.ts:

import {InMemoryDbService} from 'angular-in-memory-web-api';
export class InMemoryDataService implements InMemoryDbService {
createDb() {
const values = [
  {
    id: 0,
    title: 'Calculus',
    author: 'John Doe',
    date: '2018-05-15',
    content: 'Random text',
    category: 'Science'
  },
  {
    id: 1,
    title: 'Bulbasaur',
    author: 'John Doe',
    date: '2000-09-14',
    content: 'Random text',
    category: 'TV shows'
  },
  {
    id: 2,
    title: 'Fourier Analysis',
    author: 'John Doe',
    date: '2014-01-01',
    content: 'Random text',
    category: 'Science'
  }
];

return {values};
  }
}

Currently I am displaying the data in one of my components:

<ol class="list-unstyled mb-0">
  <li *ngFor="let value of values">{{value.title}}</li>
</ol>

How can I display only category "Science" values?

like image 802
Ventura8 Avatar asked Dec 11 '22 06:12

Ventura8


1 Answers

You can filter it out using filter of java script which return new array containing your required data , like this :

this.filteredvalues = values.filter(t=>t.category ==='Science');

and then you can iterate over this filtered array

<li *ngFor="let value of filteredvalues ">{{value.title}}</li>
like image 53
Pardeep Jain Avatar answered Dec 12 '22 19:12

Pardeep Jain