any value to iterate over. Usually an Array, Object, Map, Set, Iterator, Generator, AsyncIterator, AsyncGenerator etc. but you can also pass single values, like a number, string etc - see loopSingles
option.
the callback function to call for each item. It receives the item, the index or key and the original value (like lodash). If it returns false
or STOP
or STOP()
it breaks the loop. If you're iterating over an AsyncGenerator
or AsyncIterator
, you can also return a Promise, which will be awaited before proceeding to the next iteration.
Optional
options: Toptionsyou can optionally pass an IEachOptions
object to control which keys / idx are visited (own / inherited / enumerable etc) and more. Note: it is identical to ILoopOptions
, but with an additional async
option.
the original value, for fluent chaining. If input
was an AsyncGenerator
/AsyncIterator
, it returns a Promise<value>
resolved when iteration is over.
Iterate over the (many) items of a value, calling the callback for each item. Think of
_.each()
orArray.forEach()
, but more powerful viaz.loop()
. Hence, it works with ANY value, not just arrays and objects, but alsoMap
,Set
,Iterator
,Generator
& evenAsyncIterators
(where it returns a promise resolving when iteration ends!Just like
_.each()
, you can break out the loop by returningfalse
(as well asz.STOP
orz.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 istrue
it will throw an error, that singles aren't allowed.See
loop()
for more details on options & all behavior, sinceeach()
is built on top of it.