If I were to implement something like filterMap, I would pass in the predicate and the transformation as separate functions. However, you can use reduce for what you're doing, no need to implement a new method on Array.
filterMap
reduce
Array
const emails = customers.reduce( (prev, c) => c.active ? [ ...prev, c ] : prev, [] )
It is 80% slower, jsperf.com/array-filtermap/2
Yeah, sorry, been using array destructuring too much lately. If it's speed we're going after we can't create a new array at each iteration step.
const emails = customers.reduce( (prev, c) => { if (c.active) prev.push(c) return prev }, [] )
This way reduce can be used as map as well, nice trick but I still prefer filterMap for its smaller syntax.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
If I were to implement something like
filterMap
, I would pass in the predicate and the transformation as separate functions. However, you can usereduce
for what you're doing, no need to implement a new method onArray
.It is 80% slower, jsperf.com/array-filtermap/2
Yeah, sorry, been using array destructuring too much lately. If it's speed we're going after we can't create a new array at each iteration step.
This way reduce can be used as map as well, nice trick but I still prefer filterMap for its smaller syntax.