DEV Community

Discussion on: can't use .filter() after .forEach()

Collapse
 
vonheikemen profile image
Heiker

.map is the only option here. If the problem is process return value you can do this.

myArr.map(item => (process(item), item))
  .filter( somelogic )
  .map(item => postProcessSome(item));

If process doesn't mutate item that should be fine. You could even make a helper function.

const tap = (fn) => (value) => (fn(value), value);

array.map(tap(item => process(item)))
// ... the rest
Collapse
 
lionelrowe profile image
lionel-rowe

Comma operator for implied return from arrow function is kinda gnarly... though I guess that's a matter of personal style preference.

Collapse
 
lagsurfer profile image
Barney

TIL comma operator! Thanks!