I have an array of objects. Filtering of array of object based on selection of a dropdown value.
const itemsList=[{
"id":"123",
"problems":{
"causes":[
{
"SSEnabled": true,
"IndusEnabled": false,
"LogEnabled": true
}
]
}
},
{
"id":"234",
"problems":{
"causes":[
{
"SSEnabled": false,
"IndusEnabled": false,
"LogEnabled": true
}
]
}
}]
we have a drop-down to filter SSEnabled cause. The options of drop-down are "show","nofilter","exclude".
Need to filter the list based on dropdown selection.
If "show" option of "SSEnabled" dropdown is selected, the list item where "SSEnabled":"true" should be the result.(i.e; id:"123")
If "exclude" of "SSEnabled" cause is selected, the list item where "SSEnabled:false" should be the result.(i.e; id:"234")
If "nofilter" is selected , it should ignore the filter. (i.e; id:"123", id:"234")
filterList(filterType, filterValue, itemsList) {
// filterType : SSEnabled (type of dropdown changed)
//filterValue : show, exclude , no filter
itemsList.map((items) => {
if (
items &&
items.problems &&
items.problems.causes &&
items.problems.causes.length
) {
items.problems.causes.filter((cause) => {
if (cause[filterType] === true && filterValue === 'show') {
return true;
}
if (cause[filterType] === false && filterValue === 'exclude') {
return true;
}
});
}
});
console.log(itemsList, 'filtered List');
}
But the list is not getting filtered. Please help in filtering.
mapmethod you are not returning anything