I have an array of objects
const Data = [
{first_name: 'Ammy', city_name: 'LA', age: 22, designation: 'officer'},
{first_name: 'Dave', city_name: 'Paris', age: 23, designation: 'engineer'},
{first_name: 'Cindy', city_name: 'Tokyo', age: 32, designation: 'influencer'},
{first_name: 'Barn', city_name: 'sydney', age: 34, designation: 'lawyer'},
{first_name: 'Yolanda', city_name: 'Seoul', age: 32, designation: 'chef'},
{first_name: 'Mike', city_name: 'London', age: 42, designation: 'bartender'},
{first_name: 'Olivia', city_name: 'LA', age: 34, designation: 'officer'}
]
I also have an object containing filters
const Filters = {
first_name: ['Ammy', 'Dave', 'Mike'],
city_name: ['LA'],
age: [22, 34]
}
I have to filter the data so that all the filters are applied and it should be an and condition. For the above Data and Filters only the row containing Ammy
should be displayed. The challenging part is that the filters can have only one key or all keys. Filters can be just { first_name: ['Dave']}
I tried writing some code but it will not apply all the filters
const newData = [];
Object.entries(Filters).map(([filter, Values]) => {
const filteredData = Data.filter(item => {
if (Values.find( value => value === item[filter])) {
newData.push(item);
}
});
})