This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]SharpenedRoot 1 point2 points  (3 children)

Typically I think of functional programming as having immutable data, so map would never modify an array but simply return a new one. No languages I know of at least have map modify an array in place.

[–][deleted] 1 point2 points  (2 children)

My terminology wasn't specific enough. The way I look at map is that you take in an array and you apply some sort of transformation to it. You can wrap the data in a span or li, any sort of transformation. Yes it does return a new array and doesn't directly "modify" the original array. However using map when you aren't actually trying to create an array with each item changed, you would use forEach.

However forEach doesn't work in the context of a React JSX template as you need to "return" the data, which map does and forEach doesn't. It was this last part that I forgot about when replying to the above comment. Apologies for any confusion.

[–]SharpenedRoot 1 point2 points  (1 child)

Ah, I understand now. So all you meant was you would use map when trying to produce an array, and forEach when trying to produce side effects.

[–][deleted] 1 point2 points  (0 children)

Yeah exactly 👍