0

I am trying to apply filter option from an array.

options = [
    "car",
    "bike",
]

I have multiple filter select, that's why using an array to contain more then one option.

and let's say there is array that I want to filter.

array = [
   "car",
   "fruit",
   "keyboard",
   "computer"
]

if the option is string like options = "car" then the thing is very easy

if (!options.length) {
    return array;
} else {
    return array.filter(item => {                    
        return item === options;
    })
}

But the thing is how can I handle if the options is not string but an array?

For example I tried to put filter function inside for loop.

let len = options.length
console.log(len)
for (let i = 0; i < len; index++) {
    return array.filter(item => {                    
        return item === array[i];
    })
} 

but it didn't worked out. It's only taking first selected from an array.

yvl
  • 620
  • 7
  • 25

1 Answers1

1

You can try using Array.prototype.includes()

return options.includes(item);
Mamun
  • 66,969
  • 9
  • 47
  • 59
  • 1
    This little touch/guide helped a lot. thank you, solved it. I will accept it as answer... – yvl Feb 26 '20 at 06:36