thank you for help. I have an array of objects. And i can't find good solution except of a lot of IF's how to filter it depends on array of properties that should be checked.
const offers = [
{
isGrouped: true,
isDiscarded: false,
isCopy: false,
isWarned: false,
name: "qw",
},
{
isGrouped: false,
isDiscarded: true,
isCopy: false,
isWarned: false,
name: "qwer",
},
{
isGrouped: false,
isDiscarded: true,
isCopy: true,
isWarned: false,
name: "erw",
},
{
isGrouped: false,
isDiscarded: false,
isCopy: true,
isWarned: false,
name: "frew",
}]
and here is the array of filters
export enum Status {
DISCARDED = "Discarded",
WARNED = "Warning",
COPY = "Copy",
GROUPED = "Grouped",
}
const filters: string[] = [Status.DISCARDED, Status.COPY];
filters could include all four statuses or one, two, three or any of them.
function check(offer, filters) {
// I don't know actually how to handle filtering here.
// only one idea is to check all possible variants ex.
// if (filters.includes(Status.DISCARDED) && filters.length === 1) {
// return offer.isDiscarded
// } else if (filters.includes(Status.DISCARDED) && filters.includes(Status.COPY) && filters.length === 2) {
// return offer.isDiscarded || offer.isCopy
// }
}
offers.filter(offer => check(offer, filters))
My solutuion doesn't looks nice. Could someone take a look please?