JS Filter Array in Array using Array - (Tags)

Hello, I’m trying to make a filterable list.

The data structure is like so:

data = [
{name: ‘john’, position: ‘developer’, languages: [‘python’, ‘ruby’, ‘html’},
{name: ‘jane’, position: ‘sales’, languages: [‘python’, ‘ruby’, ‘html’},
{etc}
]

filterLanguage = [‘python’, ‘ruby’, ‘etc’]

I’m trying to filter the main data list languages based on the filters selected in the second array ‘filterLanguage’.

I feel like i’ve been going around and around for hours on this.

I’ve tried various combinations of .filter(), .includes(), .some(), .every(), .indexOf(). etc. I feel like I’m just missing something.

Any help would be greatly appreciated! Thank you :smiley:

(edited to put in correct subforum)

Can you show us exactly what you have tried?

Also, you say you want to filter the main data based on the filters selected, but can you explain how you would expect to filter the data if you specify the array you have above?

Would it filter if any of the elements were present or would if only filter if all were present?

I was actually able to get it to work after I posted this.

Here’s the solution that I used:

const filterList = () => {

  return data.filter(job => {    

    return job.languages.some(tag => {

      return filterLanguage.includes(tag);

    })

  })

}

I’ll leave this here in case anyone needs something similar. It turns out I was just missing a return and I needed to normalize the data (ie. toLowerCase()) :smiley:

1 Like