0

Does using ForEach with push to create an output array in javascript Anti-Pattern? Or is it better to use Map?

const result = [];
arr.forEach(element => {
  result.push(element.name)
})
Yassin Mo
  • 347
  • 2
  • 9
  • It's not *forbidden*, but it's pretty strange to use `forEach` when a more appropriate method exists – CertainPerformance Sep 17 '22 at 18:49
  • For a simple case like this, `map` is *cleaner*. However, if you need to perform any sort of filtering **and** mapping, often `forEach` is better than a separate `filter` and `map` combination as you reduce the amount of times you need to loop over an array. You can also use `reduce` for that if you're comfortable with it. – Rylee Sep 19 '22 at 04:08

0 Answers0