I have this array of objects
const data = [
{ id: 0, ALT_VALUE: 11, DLT_VALUE: 76 },
{ id: 1, ALT_VALUE: 80, DLT_VALUE: 48 },
{ id: 2, ALT_VALUE: 90, DLT_VALUE: 100 },
{ id: 3, ALT_VALUE: 12, DLT_VALUE: 70 },
{ id: 4, ALT_VALUE: 90, DLT_VALUE: 100 },
{ id: 5, ALT_VALUE: 13, DLT_VALUE: 49 },
{ id: 6, ALT_VALUE: 76, DLT_VALUE: 70 },
{ id: 7, ALT_VALUE: 9, DLT_VALUE: 15 },
];
and I would like to filter it dynamically based on this array of objects
const filters = [
{ parameter: "ALT_VALUE", min: 8, max: 100 },
{ parameter: "DLT_VALUE", min: 30, max: 50 },
];
based on the filters, the final result supposed to be
[
{ id: 5, ALT_VALUE: 13, DLT_VALUE: 49 },
{ id: 1, ALT_VALUE: 80, DLT_VALUE: 48 },
]
I tried with this
const result = data.filter((el) => {
return filters.filter((f) => {
if (el[f.parameter] > f.min && el[f.parameter] < f.max) {
return el;
}
});
});
But I get all of the data
[
{ id: 0, ALT_VALUE: 11, DLT_VALUE: 76 },
{ id: 1, ALT_VALUE: 80, DLT_VALUE: 48 },
{ id: 2, ALT_VALUE: 90, DLT_VALUE: 100 },
{ id: 3, ALT_VALUE: 12, DLT_VALUE: 70 },
{ id: 4, ALT_VALUE: 90, DLT_VALUE: 100 },
{ id: 5, ALT_VALUE: 13, DLT_VALUE: 49 },
{ id: 6, ALT_VALUE: 76, DLT_VALUE: 70 },
{ id: 7, ALT_VALUE: 9, DLT_VALUE: 15 }
]