• Calls the provided callbackFn function once for each element in the iterator or iterator returned by the iterable and processed in the same order as returned by the iterator. As with the arrForEach you CAN stop / break the iteration by returning -1 from thecallbackFn function.

    The order of processing is not reset if you add or remove elements to the iterator, the actual behavior will depend on the iterator imeplementation.

    If the passed iter is both an Iterable and Iterator the Iterator interface takes preceedence.

    Type Parameters

    • T

      Identifies the element type of the iterator

    Parameters

    • iter: Iterator<T, any, undefined> | Iterable<T>
    • callbackfn: ((value, count?, iter?) => number | void)

      A synchronous function that accepts up to three arguments. iterForOf calls the callbackfn function one time for each element returned by the iterator.

        • (value, count?, iter?): number | void
        • Parameters

          • value: T
          • Optional count: number
          • Optional iter: Iterator<T, any, undefined>

          Returns number | void

    • Optional thisArg: any

      An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, null or undefined the iterator will be used as the this value.

    Returns void

    Remarks

    If Symbols are NOT supported then the iterable MUST be using the same polyFill for the well know symbols, if you are targetting a mixed environment you SHOULD either

    • only use the polyfill Symbol's provided by this library
    • ensure that you add any symbol polyfills BEFORE these utilities iterForOf expects a synchronous function. iterForOf does not wait for promises. Make sure you are aware of the implications while using promises (or async functions) as forEach callback.

    Since

    0.4.2

    Throws

    Any exception thrown while processing the iterator

    Example

    const items = {
    'item1': 'value1',
    'item2': 'value2',
    'item3': 'value3
    };
    const copyItems = [];

    iterForOf(items, (item) => {
    copyItems.push(item);
    // May return -1 to abort the iteration
    });