How to understand Ruby's .each and .map

后端 未结 5 539
予麋鹿
予麋鹿 2021-01-18 21:00

I am having trouble understanding the differences between map and each, and where and when to use them.

I read \"What does map do?\" and \

5条回答
  •  长发绾君心
    2021-01-18 21:30

    The difference is that each is performing an action on each element in the array, returning the original array. The action performed possibly mutated the element.

    Whereas map is performing an action on each element in the array and returning its result as an array.

提交回复
热议问题