1

Apologies if this has been asked before and if what I am asking doesn't make sense please let me know and I will do my best to clarify.

What I am looking for is a function that works the same way as the .includes() function does, but I would like it to accept an array. It seems that the "includes" function only looks for 1 needle in a haystack instead of multiple needles.

Any help with this would be great.

For Context: I am working in Vuejs, and I have a computed property that is taking an array of objects and each object contains another array of colors. To sort I am adding color codes to the selectedColors array and would like to filter my searchArray results based on the selectedColors array.

Below is what I have in my computed property as of now... and obviously when the selectedColors array has anything in it, no results are returned.

colorArray: function() {
      return this.searchArray.filter(station => {
        if (this.selectedColors) {
          return station.colors.includes(this.selectedColors)
        } else {
          return this.searchArray
        }
      })
    }

Here is an example of my searchArray:

this.searchArray: [
    {title: 'first', colors: ['BL', 'YL']},
    {title: 'second', colors: ['YL']},
    {title: 'third', colors: ['OR', 'GR']},
]

Here is an example of my selectedColorsArray:

this.selectedColors: ['BL', 'YL']

Any help with this is greatly appreciated! Thanks

Phil
  • 157,677
  • 23
  • 242
  • 245
JoRoFo
  • 71
  • 5
  • 1
    Your `else` should just `return true` since you're within the `filter()` callback – Phil Nov 14 '19 at 05:16
  • Good call. I have updated my code to reflect that change. Thanks – JoRoFo Nov 14 '19 at 05:18
  • 1
    What logic do you want to apply? `colors` contains **all** entries in `selectedColors` or **any** entry? – Phil Nov 14 '19 at 05:18
  • I would like it to be all inclusive. So the ```searchArray``` would return all results based on what is in the ```selectedColors``` array – JoRoFo Nov 14 '19 at 05:20
  • 1
    That's not what I asked you. Say `selectedColors` contains `['a', 'b']` and you have `station1` with colors `['a', 'b', 'c']` and `station2` with colors `['a', 'c']`, do they both get returned or only `station1`? – Phil Nov 14 '19 at 05:20
  • Apologies. I misread, ```colors``` can contains ANY entry – JoRoFo Nov 14 '19 at 05:22

0 Answers0