I want to filter through an array and retrieve items if their filters match. However it does not work.
const wanted=["apple"]
const items = [
{
"name": "Iphone"
"filters": ["phone","apple"]
},
{
"name": "samsung12"
"filters": ["samsung12","samsung"]
},
]
let desiredList = items.filter((val) => wanted.includes(val.filters));
Any ideas on how i could get this to work?
>Solution :
You would need to use some to check if any of the filters contains a value in wanted:
const wanted = ["apple"]
const items = [{
"name": "Iphone",
"filters": ["phone", "apple"]
}, {
"name": "samsung12",
"filters": ["samsung12", "samsung"]
}];
let desiredList = items.filter((item) => item.filters.some((f) => wanted.includes(f)));
console.log(desiredList);