Filter Array Of Objects By Property Value
I'm new with Angular and observables. I'm getting from a JSON file a list of products. products.service getProducts(): Observable{ return this._http.get
Solution 1:
Try this.
let o = { "products": [
{
"quantity": 308,
"price": "$8,958",
"available": false,
"sublevel_id": 3,
"name": "aute",
"id": "58b5a5b1b6b6c7aacc25b3fb"
},
{
"quantity": 891,
"price": "$5,450",
"available": true,
"sublevel_id": 3,
"name": "mollit",
"id": "58b5a5b117bf36cf8aed54ab"
},
{
"quantity": 698,
"price": "$17,001",
"available": false,
"sublevel_id": 10,
"name": "eiusmod",
"id": "58b5a5b18607b1071fb5ab5b"
}
]}
let filtered = o.products.filter(p => p.available===true)
console.log(filtered);
Solution 2:
This should work.
getProducts(): void{
this.productsService.getProducts()
.subscribe(products =>this.products = products.filter(product=>product.available==true));
}
Solution 3:
use the filter operator inside the pipe
and do the normal subscription
filterProducts(): Observable<Product[]>{
return this.getProducts()
.pipe(
filter(products => product.available )
)
}
Post a Comment for "Filter Array Of Objects By Property Value"