Filter an array by an array

lindsay
lindsay
Aug 28, 2017 · 1 min read

I recently wanted to filter an array of arrays by an array (whew). I had an arrays of ids and I wanted to “filter” them using another array. I used arr.filter() filter along with arr.some()some and arr.includes() .

the function:

exports.filter = function(arr, filter = []) {
if (filter.length == 0) { // if no filter, return entire arr
return arr;
} else {
return arr.filter(ele => {
return ele.some(innerEle => filter.includes(innerEle));
});
}
}

and some chai tests:

)

Written by

lindsay

this is becoming a place i list TIL… twitter: @lynzt

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade