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

    Class ArrayIterator<T>

    An iterator class for arrays with chainable operations and lazy evaluation

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

    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

      The type of elements in the array. 数组元素的类型

    Index

    Constructors

    • Creates a new ArrayIterator instance

      创建一个新的 ArrayIterator 实例

      Type Parameters

      • T = unknown

        The type of elements in the array. 数组元素的类型

      Parameters

      • array: T[]

        The source array to iterate. 要迭代的源数组

      Returns ArrayIterator<T>

    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 实例

      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

      • predicate: ArrayIteratorPredicate<T>

        The function to test for each element. 用于测试每个元素的函数

      Returns boolean

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

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

      根据断言函数过滤元素

      Parameters

      • predicate: ArrayIteratorPredicate<T>

        The predicate function. 断言函数

      Returns ArrayIterator<T>

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

      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

      • predicate: ArrayIteratorPredicate<T>

        The function to test for each element. 用于测试每个元素的函数

      Returns T | undefined

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

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

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

      Parameters

      • callback: ArrayIteratorCallback<T>

        The function to execute for each element. 为每个元素执行的函数

      Returns void

      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

      • transform: ArrayIteratorTransform<T, R>

        The transform function. 转换函数

      Returns ArrayIterator<R>

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

      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

      • predicate: ArrayIteratorPredicate<T>

        The function to test for each element. 用于测试每个元素的函数

      Returns boolean

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

      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 实例

      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]