Ajinkya Kulkarni
Ajinkya Kulkarni

Reputation: 51

Filter Json Array Data in Typescript

I have recently started working on angular and I need to filter data from an JSON array. Basically I want to display names whose id is less than 100. Below is not working for me.

list : any;

getOptionList(){
this.callingService.getData().pipe(
  filter((l) => l.id < 100)
).subscribe(
  (l) => { this.list = l;} 
)
}
}

Json Array from calling Service

[
  {
    "id":1,
    "name": "A"
  },
  {
    "id":2,
    "name": "B"
  },
  ...
]

Upvotes: 0

Views: 5133

Answers (1)

Aman Gojariya
Aman Gojariya

Reputation: 1429

this.callingService.getData().subscribe((res: any) => {
      let filteredArr = res.filter(data => data.id < 100);
      //Here you got the filtered array.
});

It will work for you.

Upvotes: 2

Related Questions