I’m trying to filter some products with the categories (by the woocommerce API) but i have a little trouble to do it. do you have the right way to do it? thanks
My array from the API :
{id: 199, name: "Dark Suit", slug: "dark-suit", permalink: "https://caisse.diliko.fr/produit/dark-suit/", date_created: "2020-08-20T10:53:06", …}
categories: Array(2)
0:
id: 30
name: "Clothing"
slug: "clothing"
__proto__: Object
1: {id: 31, name: "Men's Clothing", slug: "mens-clothing"}
length: 2
__proto__: Array(0)
And my filter function:
export const getProductsById = (products, id) => (
products.filter(product => product.categories.name == id)
);
2
Answers
Here’s what you want, I reckon. A filter that checks whether any of the categories of the product has the id you’re looking for.
You need to add one more filter/iteration on
products.category
because it is an array.products.filter(product => product.category.filter(category => category.name === id