I want filter an array in javascript with condition or any other.
My code:
var search = ['football', 'rugby']; // search.length can be greater
var records = [{name: 'thomas', hobbies: ['rugby', 'tennis']},
{name: 'bob', hobbies: ['swimming', 'tennis']},
{name: 'gloria', hobbies: ['football', 'tennis', 'golf', 'swimming']}]
for(let i = 0; i < search.length; i++) {
var v = search[i];
if (records.length > 0) records = records.filter((r) => r.hobbies.includes(v));
}
var searchfilter = 'r.hobbies.includes(search[0] || r.hobbies.includes(search[1] ... || r.hobbies.includes(search[x])'
searchfilter was the regular expression. In my result (records) is ‘gloria’ only. Understandable!
Any ideas?
New contributor
bernd62 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1