In the below code, I am trying to reduce the persons array only to persons which match perfectly with filters object.
Currently the output is an array of objects that have a match in ANY of the object keys. How can I adjust it to return only the second person - the one that has a perfect match with ALL filters?
var persons = [
{city: 'London', 'age': 18, 'name': 'Josh'},
{city: 'London', 'age':42, 'name': 'Josh'},
{city: 'New York', 'age': 42, 'name': 'Mike'},
{city: 'Tokyo', 'age': 33, 'name': 'Kazuna'},
]
var filters = {
city: 'London',
age: 42,
name: 'Josh',
}
function filter(person){
for (let key of Object.keys(filters)) {
if (person[key] == filters[key]) {
return person
}
}
}
var filtered = persons.filter((person) => filter(person))
console.log(filtered)