My data consists of an array of objects:
let allData = [
{title:"Adams",age:24,gender:"male"},
{title:"Baker",age:24,gender:"female"},
{title:"Clark",age:23,gender:"male"},
{title:"Davis",age:23,gender:"female"},
{title:"Ghosh",age:23,gender:"female"},
{title:"Adams",age:23,gender:"male"},
{title:"Irwin",age:25,gender:"male"},
]
Additionally, I have a filter object with various filter terms:
let filters = {
title:{filterTerm:[]},
gender:{filterTerm:["male"]},
age:{filterTerm:[23]}
}
I attempted to apply multiple filters to the 'allData' using Array.some and Array.includes but encountered issues. Here is the code snippet I used:
const getFilterRows = (rows, filters) => {
let filterCols = Object.keys(filters)
if (filterCols.length == 0) {
return rows
}
else {
let filteredR = rows.filter(i => {
return filterCols.some((s) => {
return filters[s].filterTerm.includes(i[s])
})
});
return filteredR
}
}
let filtered = getFilterRows(allData,filters)
The expected output after applying the filters should be:
[
{title:"Clark",age:23,gender:"male"},
{title:"Adams",age:23,gender:"male"},
]