Function each

Iterate over the (many) items of a value, calling the callback for each item. Think of _.each() or Array.forEach(), but more powerful via z.loop(). Hence, it works with ANY value, not just arrays and objects, but also Map, Set, Iterator, Generator & even AsyncIterators (where it returns a promise resolving when iteration ends!

Just like _.each(), you can break out the loop by returning false (as well as z.STOP or z.STOP() ;-))

z.isSingle values are also supported:

  • with loopSingles: true (default), it yields a single iteration of the value itself (value passed to the callback once). This allows functional programming philosophy, where all values are "enclosed" and can be mapped over.

  • Otherwise (loopSingles: true) they are ignored (i.e a 0 iterations loop), but it won't choke on them.

  • By default, options.strict: false but if strict is true it will throw an error, that singles aren't allowed.

See loop() for more details on options & all behavior, since each() is built on top of it.