I have the following
let foo = ['public', 'private', 'secured', 'unsecured']; // value to search, it can be any combination
['secured', 'unsecured'], ['public', 'secured'] etc...
ARRAY
[
{ id: 1, isPrivate: true, isSecured: true },
{ id: 2, isPrivate: false, isSecured: true },
{ id: 3, isPrivate: true, isSecured: false },
{ ID: 4, isPrivate: false, isSecured: false }
];
[...items].filter(x => filterLabel(x, foo));
filterLabel(x, foo): boolean {
switch (foo[0]) {
case 'private': return x.isPrivate;
case 'public': return !x.isPrivate;
case 'secured': return x.isSecured;
case 'unsecured': return !x.isSecured;
default: return true;
}
This WORKS but it only filters by the first item of the array, i can't figure out how can i filter by using any combination of foo
- Example:
['public', 'secured', 'unsecured'];
This would filter the array [...items] by item.isPrivate = false, item.isSecured = true, item.isSecured = false
- Example:
['public', 'unsecured'];
This would filter the array [...items] by item.isPrivate = false, item.isSecured = false
- Example:
['private', 'unsecured'];
This would filter the array [...items] by item.isPrivate = true, item.isSecured = false
PD: it can be solved by comparing any of the combination but i want to avoid this
const hash = new Set(foo);
const isPrivate = hash.has('private');
const isPublic = hash.has('public');
const isSecured = hash.has('secured');
const isUnsecured = hash.has('unsecured');
if (isPrivate && !isPublic && !isSecured && !isUnsecured) {
return item.isPrivate;
}
if (!isPrivate && isPublic && !isSecured && !isUnsecured) {
return !item.isPrivate;
}
if (!isPrivate && !isPublic && isSecured && !isUnsecured) {
return item.isSecured;
}
// and so on... with all the combinations
[...items].filter?privateandpublicat the same time? orsecuredandunsecured?