Essentially, I am trying to filter out specific indices that are not in criteria array. Data that I am getting from JSOn file looks like this:
let data = [
{
"category": ["business"],
"title": "some title"
},
{
"category": ["travel", "business"],
"title": "some title"
},
{
"category": ["sport"],
"title": "some title"
},
{
"category": ["business", "sport"],
"title": "some title"
}
]
The array that should filter category of each object from the data above:
var criteria = ["business", "travel"]
I've tried numerous options, the ones that I've listed below are only the ones that either return something or not bring any errors, however, both methods do not bring desired outcome.
const filtered = data.filter((item) =>
// this returns only first "business" category while ignoring the rest
criteria.find(element => item.category == element)
)
const filtered = data.filter((item) =>
// this simply returns all data entries, even the ones that are not in criteria array.
criteria.filter(element => item.category == element)
)
const filtered = spanishData.filter(function (item) {
// returns all data entries as well
return criteria.indexOf(item.category) === -1
})
How can I filter the data > category array according to the criteria array?
Thank you