WHITECOLOR
WHITECOLOR

Reputation: 26170

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.

Upvotes: 0

Views: 127

Answers (1)

user3743222
user3743222

Reputation: 18665

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']))

Upvotes: 4

Related Questions