More general looping

The preceding examples that we've seen simply loop through arrays, doing some work. However, sometimes you need to do a loop, but the required process doesn't really fit map() or reduce(). So what can be done in such cases? There is a forEach() method that can help.

You must provide a callback that will receive the value, the index, and the array on which you are operating. (The last two arguments are optional.) JavaScript will take care of the loop control, and you can do whatever you want at each step. For instance, we can program an object copy method by using ...

Get Mastering JavaScript Functional Programming - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.