I have json file: products.json:
[
{
"id": "1",
"category": "Fruit from USA",
"name": "Banana",
},
{
"id": "2",
"category": "Fruit from Brazil",
"name": "Long Banana",
},
{
"id": "3",
"category": "Vegetable",
"name": "Carrot",
},
{
"id": "4",
"category": "Car from USA",
"name": "Ford",
},
{
"id": "5",
"category": "Car from Germany",
"name": "Audi",
},
{
"id": "6",
"category": "Train from Italy",
"name": "Pendolino",
},
]
Then I have an array
testMatch.match: ['Car', 'Fruit'].
I want to filter out products.json to return only objects that have category starts with any of elements of matchCategory in ES6
What i have so far is:
const productList = products.filter(filteredCategory => filteredCategory.category.startsWith(testMatch.match));
But it does not work if there is more than 1 element in testMatch.match and if there is none – it returns all products and not none.