{
data: [
{
user: 'Vasya',
rating: 35,
disabled: false
},
{
user: 'Petya',
rating: 24,
disabled: false
},
{
user: 'Dmitry',
rating: 22,
disabled: true
},
],
condition: {
exclude: [disabled: true]
}
}
[{"user": "Vasya", "rating": 45, "disabled": false},
{"user": "Petya", "rating": 24, "disabled": false}]
const condition = {
exclude: [{
disabled: true
}],
include: [{
rating: 24
}]
}
const getFiltered = (data, filters) => {
const {
include,
exclude
} = filters;
let filteredData = data;
if (include) {
const includeEntries = include.flatMap(filter => Object.entries(filter));
filteredData = filteredData.filter(value =>
includeEntries.every(([key, valueFilter]) => value[key] === valueFilter)
);
}
if (exclude) {
const excludeEntries = exclude.flatMap(filter => Object.entries(filter));
filteredData = filteredData.filter(value =>
excludeEntries.every(([key, valueFilter]) => value[key] !== valueFilter)
);
}
return filteredData;
}
condition: {
exclude: [{disabled: true}, {rating: 24}]
}
}
const { condition, data } = users;
const commonCondition = condition
.exclude
.reduce((acc, condition) => ({...acc, ...condition }), {});
const filteredUsers = data.filter((user) => { // [{"user": "Dmitry", "rating": 22, "disabled": false}]
return Object
.entries(commonCondition)
.every(([key, value]) => value !== user[key])
});