WHITECOLOR WHITECOLOR - 7 months ago 33
Javascript Question

Rxjs (4,5) shorthand for filtering values

Is there a shortcut for filtering values:

Something like:

.filterValues(['POP', 'PUSH'])


Instead of this:

.filter(action => action == 'POP' || action === 'PUSH')


?

Searched through docs, maybe missed something.

Answer

There is none that I know of. That said, you can always write your own utility function and/or use underscore/lodash and/or use ramda or other functional library. _.eq could for example be used if you have only one value in your filter values array. Array.prototype.indexOf, or R.indexOf can also be used in conjunction with R.compose and R.gt. But the dummiest and simplest way to go is :

var utils = {}
utils.filterValues = function (arr) { 
  return function (value) { return arr.indexOf(value) > -1}
}

and then .filter(utils.filterValues(['POP', 'PUSH']))