DEV Community

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

Collapse
 
mdor profile image
Marco Antonio Dominguez

You can even use a simple reduce

myArray
  . reduce((acc, item) => {
    // Filter if met some condition, and add the proccesed item
    if (somelogic)  acc.push(process.item)
   return acc;
  }, [])
 .forEach(item => /*anything */ )

/* Compact should be */
myArray
  . reduce(
    (acc, item) => (somelogic && acc.push(process(item)), acc), []
  )
  .forEach(item => /*anything */ )

Collapse
 
lagsurfer profile image
Barney

Nice! Very cool utilization of reduce I like it.