Skip to content
Advertisement

How to pass array parameter to includes method in JavaScript [closed]

I have two arrays:

const tags=["filter1","filter2","filter3"]
const data = [{name:"aaaaa",tags:["filter1","filter2","filter3","filter4"]},{name:"bbbb",tags:["filter"]}]

I need something like this, but I can`t pass array into includes method

const newData = data.filter(i=>i.tags.includes(tags) === true) //[{name:"aaaaa",tags:["filter1","filter2","filter3","filter4"]

Advertisement

Answer

You probably want either one of these two:

const newData = data.filter(d => tags.every(t => d.tags.includes(t)));
const newData = data.filter(d => tags.some(t => d.tags.includes(t)));
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement