DEV Community

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

Collapse
 
lionelrowe profile image
lionel-rowe • Edited

What's wrong with this? Easy to reason about and the same total number of lines, though line count is a bit of an artificial metric - better to prioritize readability.

myArr.forEach(item => {
  process(item)
  if (someLogic(item)) postProcess(item)
})
Collapse
 
lagsurfer profile image
Barney

For readability this is definitely the winner and it only iterates over the array once.