@pengzhanbo/utils - v3.7.3
    Preparing search index...

    Class ArrayIterator<T>

    An iterator class for arrays with chainable operations and lazy evaluation

    支持链式操作和惰性计算的数组迭代器类

    Each chained operation (.filter(), .map(), .drop(), .take()) creates a shallow copy of the current operation list (O(k) where k is the number of chained calls). Excessive chaining may cause quadratic overhead. Consider limiting chain depth and combining filter/map logic where possible.

    每次链式调用(.filter()、.map()、.drop()、.take())都会浅拷贝当前操作列表(O(k),k 为链式调用次数)。大量链式调用可能产生平方级开销。建议控制链式调用深度,并在可能时合并 filter/map 逻辑。

    const result = new ArrayIterator([1, 2, 3, 4, 5])
    .filter((v) => v > 2)
    .map((v) => v * 2)
    .take(2)
    .toArray()
    // => [6, 8]

    Type Parameters

    • T = unknown
    Index

    Constructors

    Methods

    • Implements the iterable protocol, allowing ArrayIterator to be used in for...of loops, spread operator, and other iterable consumers

      实现可迭代协议,允许 ArrayIterator 用于 for...of 循环、展开运算符及其他可迭代消费者

      Returns Generator<T>

      An iterator for the processed elements. 处理后元素的迭代器

      const iterator = new ArrayIterator([1, 2, 3])
      for (const value of iterator) {
      console.log(value)
      }
    • Drops the first N elements

      跳过前 N 个元素

      Parameters

      • count: number

        The number of elements to drop. 要跳过的元素数量

      Returns ArrayIterator<T>

      A new ArrayIterator instance with the drop operation. 带有跳过操作的新 ArrayIterator 实例

      When count is not a non-negative integer. 当 count 不是非负整数时抛出。

      new ArrayIterator([1, 2, 3, 4, 5])
      .drop(2)
      .toArray()
      // => [3, 4, 5]
    • Tests whether all elements pass the test implemented by the provided function

      测试所有元素是否都通过了由提供的函数实现的测试

      Parameters

      Returns boolean

      true if all elements pass the test, false otherwise. 如果所有元素都通过测试则返回 true,否则返回 false

      When predicate is not a function. 当 predicate 不是函数时抛出。

      new ArrayIterator([2, 4, 6]).every((v) => v % 2 === 0)
      // => true
    • Filters elements based on a predicate function

      根据断言函数过滤元素

      Parameters

      Returns ArrayIterator<T>

      A new ArrayIterator instance with the filter operation. 带有过滤操作的新 ArrayIterator 实例

      When predicate is not a function. 当 predicate 不是函数时抛出。

      new ArrayIterator([1, 2, 3, 4])
      .filter((v) => v > 2)
      .toArray()
      // => [3, 4]
    • Returns the value of the first element that satisfies the provided testing function

      返回满足提供的测试函数的第一个元素的值

      Parameters

      Returns T | undefined

      The value of the first element that passes the test, or undefined if no element passes. 通过测试的第一个元素的值,如果没有元素通过则返回 undefined

      When predicate is not a function. 当 predicate 不是函数时抛出。

      new ArrayIterator([1, 2, 3, 4]).find((v) => v > 2)
      // => 3
    • Executes a provided callback function once for each array element

      对数组中的每个元素执行一次提供的回调函数

      Parameters

      Returns void

      When callback is not a function. 当 callback 不是函数时抛出。

      new ArrayIterator([1, 2, 3])
      .forEach((value, index) => console.log(value, index))
    • Transforms elements using a transform function

      使用转换函数转换元素

      Type Parameters

      • R

        The type of transformed elements. 转换后的元素类型

      Parameters

      Returns ArrayIterator<R>

      A new ArrayIterator instance with the map operation. 带有映射操作的新 ArrayIterator 实例

      When transform is not a function. 当 transform 不是函数时抛出。

      new ArrayIterator([1, 2, 3])
      .map((v) => v * 2)
      .toArray()
      // => [2, 4, 6]
    • Tests whether at least one element passes the test implemented by the provided function

      测试是否至少有一个元素通过了由提供的函数实现的测试

      Parameters

      Returns boolean

      true if at least one element passes the test, false otherwise. 如果至少有一个元素通过测试则返回 true,否则返回 false

      When predicate is not a function. 当 predicate 不是函数时抛出。

      new ArrayIterator([1, 2, 3]).some((v) => v % 2 === 0)
      // => true
    • Takes the first N elements

      获取前 N 个元素

      Parameters

      • limit: number

        The maximum number of elements to take. 要获取的最大元素数

      Returns ArrayIterator<T>

      A new ArrayIterator instance with the take operation. 带有获取操作的新 ArrayIterator 实例

      When limit is not a non-negative integer. 当 limit 不是非负整数时抛出。

      new ArrayIterator([1, 2, 3, 4, 5])
      .take(3)
      .toArray()
      // => [1, 2, 3]
    • Executes all chained operations and returns the final result array

      执行所有链式操作并返回最终结果数组

      Returns T[]

      An array of the processed elements. 处理后的元素数组

      new ArrayIterator([1, 2, 3]).toArray()
      // => [1, 2, 3]