1

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

  1. Example: ['public', 'secured', 'unsecured'];

This would filter the array [...items] by item.isPrivate = false, item.isSecured = true, item.isSecured = false

  1. Example: ['public', 'unsecured'];

This would filter the array [...items] by item.isPrivate = false, item.isSecured = false

  1. 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
4
  • do you have a full example? btw, why spreading [...items].filter? Commented Mar 11, 2021 at 14:10
  • what do you need?, i put an example array, and it's just to clarify Commented Mar 11, 2021 at 14:17
  • how can it be private and public at the same time? or secured and unsecured? Commented Mar 11, 2021 at 14:33
  • they can search for both Commented Mar 11, 2021 at 14:46

1 Answer 1

1

You could filter the array ba taking off contradictional labels and take for the rest a function.

const
    filterLabel = filter => {
        const
            p = ['private', 'public'],
            s = ['secured', 'unsecured'],
            fn = {
                private: ({ isPrivate }) => isPrivate,
                public: ({ isPrivate }) => !isPrivate,
                secured: ({ isSecured }) => isSecured,
                unsecured: ({ isSecured }) => !isSecured
            };

        if (p.every(v => filter.includes(v)) filter = filter.filter(v => !p.includes(v));
        if (s.every(v => filter.includes(v)) filter = filter.filter(v => !s.includes(v));

        return o => filter.every(k => fn[k](o));
    },
    foo = ['public', 'private', 'secured', 'unsecured'],
    result = items.filter(filterLabel(foo));
Sign up to request clarification or add additional context in comments.

1 Comment

wow you are a master! you have a free beer anytime!! <3

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.