I have the following array
let tableA=[{id:'1', name:'name1',age:'31',gender:'male',class='B'},
{id:'2', name:'name2',age:'38',gender:'male',class='A'},
{id:'3', name:'name3',age:'35',gender:'male',class='C'},
{id:'4', name:'name4',age:'20',gender:'female',class='B'},
{id:'5', name:'name5',age:'19',gender:'female',class='A'},
{id:'6', name:'name6',age:'31',gender:'male',class='A'}
];
and this array for filters
let filters = [{type:'gender',value:"male"}, {type:'age',value:"31"}];
I'm trying to filter tableA with the values of table filters, all filters must match. Filters are not static, it means that filters can include json files for all table keys.
So far I'm trying to filter table_A with the code above.
let filtered_table=[];
tableA.forEach(row => {
filters.map((item) => {
if(row[item.type]==item.value){
filtered_table.push(row);
}
});
});
console.log(filtered_table);
the result of the code above is:
[ { id: '1', name: 'name1', age: '31', gender: 'male', class: 'B' },
{ id: '1', name: 'name1', age: '31', gender: 'male', class: 'B' },
{ id: '2', name: 'name2', age: '38', gender: 'male', class: 'A' },
{ id: '3', name: 'name3', age: '35', gender: 'male', class: 'C' },
{ id: '6', name: 'name6', age: '31', gender: 'male', class: 'A' },
{ id: '6', name: 'name6', age: '31', gender: 'male', class: 'A' } ]
How I can get back only the row that match for all of the filters_table items combined? For this example I'm expecting this:
[ { id: '1', name: 'name1', age: '31', gender: 'male', class: 'B' },
{ id: '6', name: 'name6', age: '31', gender: 'male', class: 'A' } ]