Wondering what’s next for npm?Check out our public roadmap! »

    array-iterate
    TypeScript icon, indicating that this package has built-in type declarations

    2.0.0 • Public • Published

    array-iterate

    Build Coverage Downloads Size

    Array#forEach() with the possibility to change the next position.

    Install

    This package is ESM only: Node 12+ is needed to use it and it must be imported instead of required.

    npm:

    npm install array-iterate

    Use

    import {arrayIterate} from 'array-iterate'
    
    var isFirst = true
    var thisArg = {hello: 'world'}
    
    arrayIterate([1, 2, 3, 4], callbackFn, thisArg)
    
    function callbackFn(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]

    API

    This package exports the following identifiers: arrayIterate. There is no default export.

    arrayIterate(values, callbackFn[, thisArg])

    Perform the specified action for each element in an array, so works just like Array#forEach(). When callbackFn returns a number, moves to the element at that index next.

    Parameters
    • values (Array-like thing) — Values to iterate over
    • callbackFn (Function) — A function that accepts up to three arguments
    • thisArg (*, optional) — An object to which the this keyword can refer in callbackFn. If thisArg is omitted, undefined is used as the this value

    function callbackFn(value, index, values)

    Callback given to iterate.

    Parameters
    • value (*) — Current iteration
    • index (number) — Position of value in values
    • values (Array-like thing) — Currently iterated over
    Context

    thisArg when given to arrayIterate or undefined

    Returns

    number (optional) — Position to go to next.

    License

    MIT © Titus Wormer

    Install

    npm i array-iterate

    DownloadsWeekly Downloads

    282,620

    Version

    2.0.0

    License

    MIT

    Unpacked Size

    8.26 kB

    Total Files

    5

    Last publish

    Collaborators

    • avatar