$ cnpm install array-iterate
Array#forEach() with the possibility to change the next position.
npm:
npm install array-iterate
var iterate = require('array-iterate')
var isFirst = true
var context = {hello: 'world'}
iterate([1, 2, 3, 4], callback, context)
function callback(value, index, values) {
console.log(this, value, index, values)
if (isFirst && index + 1 === values.length) {
isFirst = false
return 0
}
}
Yields:
{hello: 'world'}, 1, 0, [1, 2, 3, 4]
{hello: 'world'}, 2, 1, [1, 2, 3, 4]
{hello: 'world'}, 3, 2, [1, 2, 3, 4]
{hello: 'world'}, 4, 3, [1, 2, 3, 4]
{hello: 'world'}, 1, 0, [1, 2, 3, 4]
{hello: 'world'}, 2, 1, [1, 2, 3, 4]
{hello: 'world'}, 3, 2, [1, 2, 3, 4]
{hello: 'world'}, 4, 3, [1, 2, 3, 4]
iterate(values, callback[, context])Works just like Array#forEach(), but when callback returns a
number, iterates over the item at number next.
values (Array-like thing) — Values to iterate overcallback (Function) — Callback given to iteratecontext (*, optional) — Context object to use when invoking callbackfunction callback(value, index, values)Callback given to iterate.
value (*) — Current iterationindex (number) — Position of value in valuesvalues (Array-like thing) — Currently iterated overcontext, when given to iterate.
number (optional) — Position to go to next.
Copyright 2013 - present © cnpmjs.org