Search code examples
javascriptarrayslodashramda.js

Filtering an Array based on another Boolean array


Say I have two arrays:

const data = [1, 2, 3, 4]
const predicateArray = [true, false, false, true]

I want the return value to be:

[1, 4]

So far, I have come up with:

pipe(
  zipWith((fst, scnd) => scnd ? fst : null)),
  reject(isNil) 
)(data, predicateArray)

Is there a cleaner / inbuilt method of doing this?

Solution in Ramda is preferred.


Solution

  • This works in native JS (ES2016):

    const results = data.filter((d, ind) => predicateArray[ind])