My data set consists of 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'}
]
In addition to the data array, I have an object containing filters
const Filters = {
first_name: ['Ammy', 'Dave', 'Mike'],
city_name: ['LA'],
age: [22, 34]
}
The task at hand is to filter the data using all specified conditions (and condition). For the given Data and Filters, only the row with Ammy
should be displayed. The complexity arises from the fact that the filters can apply to a single key or multiple keys. It could be just { first_name: ['Dave']}
Previous attempts in writing filtering code did not consider applying all filters
const newData = [];
Object.entries(Filters).map(([filter, Values]) => {
const filteredData = Data.filter(item => {
if (Values.find( value => value === item[filter])) {
newData.push(item);
}
});
})