es-stl - v3.0.2
    Preparing search index...

    Chainable iterator helper for collection-like objects.

    An iterator delegates actual traversal to its owner, allowing collection implementations to control ordering while sharing common helpers such as map, filter, and reduce.

    const dict = new Dictionary({ a: 1, b: 2, c: 3 });

    dict.iterator()
    .filter((value) => value > 1)
    .map((value) => value * 10);
    // => [20, 30]

    Type Parameters

    • V

      Value type yielded by the iterator.

    • K = number

      Key type yielded with each value.

    Implements

    Index

    Constructors

    • Creates an iterator.

      Type Parameters

      • V

        Value type yielded by the iterator.

      • K = number

        Key type yielded with each value.

      Parameters

      • owner: IteratorOwner<V, K>

        Object that performs traversal.

      • reverse: boolean = false

        Whether traversal should run in reverse order.

      Returns Iterator<V, K>

      const dict = new Dictionary({ a: 1, b: 2 });
      const iterator = new Iterator(dict);

      iterator.map((value) => value);
      // => [1, 2]

    Properties

    _owner: IteratorOwner<V, K>
    _reverse: boolean

    Methods

    • Delegates traversal to the bound owner.

      Parameters

      • breakFlag: boolean

        Callback result that should stop traversal early.

      • callback: IterateCallback<V, K>

        Callback invoked for each value.

      Returns boolean

      breakFlag when traversal stops early, otherwise !breakFlag.

    • Tests whether every value passes callback.

      Parameters

      Returns boolean

      true when all values pass the predicate.

      const dict = new Dictionary({ a: 2, b: 4 });

      dict.iterator().every((value) => value % 2 === 0);
      // => true
    • Filters values into a new array.

      Parameters

      Returns V[]

      Values that pass the predicate in iteration order.

      const dict = new Dictionary({ a: 1, b: 2, c: 3 });

      dict.iterator().filter((value) => value >= 2);
      // => [2, 3]
    • Finds the first value that passes callback.

      Parameters

      Returns V | undefined

      First matching value, or undefined when none match.

      const dict = new Dictionary({ a: 1, b: 2 });

      dict.iterator().find((value) => value > 1);
      // => 2
    • Finds the zero-based iteration index of the first matching value.

      Parameters

      Returns number

      First matching iteration index, or -1 when none match.

      const dict = new Dictionary({ a: 1, b: 2 });

      dict.iterator().findIndex((value) => value === 2);
      // => 1
    • Invokes callback for each value.

      Parameters

      Returns void

      Iteration can stop early when callback explicitly returns false.

      const dict = new Dictionary({ a: 1, b: 2 });
      const seen: number[] = [];

      dict.iterator().forEach((value) => {
      seen.push(value);
      });
      // seen === [1, 2]
    • Maps each value to a new array.

      Type Parameters

      • T

        Result value type.

      Parameters

      Returns T[]

      Mapped values in iteration order.

      const dict = new Dictionary({ a: 1, b: 2 });

      dict.iterator().map((value, key) => `${key}:${value}`);
      // => ['a:1', 'b:2']
    • Reduces values by using the first iterated value as the initial accumulator.

      Parameters

      Returns V | undefined

      Reduced value, or undefined when the iterator is empty.

      const dict = new Dictionary({ a: 1, b: 2, c: 3 });

      dict.iterator().reduce((sum, value) => sum + value);
      // => 6
    • Reduces values by using init as the initial accumulator.

      Type Parameters

      • T

        Accumulator type.

      Parameters

      • callback: ReduceCallback<V, K, T>

        Reducer invoked for each value.

      • Optionalinit: T

        Initial accumulator value.

      Returns T | undefined

      Reduced value.

      const dict = new Dictionary({ a: 1, b: 2 });

      dict.iterator().reduce((sum, value) => sum + value, 10);
      // => 13
    • Tests whether at least one value passes callback.

      Parameters

      Returns boolean

      true when any value passes the predicate.

      const dict = new Dictionary({ a: 1, b: 3 });

      dict.iterator().some((value) => value > 2);
      // => true
    • Default traversal implementation for unbound iterators.

      Parameters

      Returns boolean

      Regular users normally call traversal helpers such as forEach instead of this method. Collection owners provide the concrete traversal implementation.

      Always throws because concrete owners must implement traversal.