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

    Class ObjectIterator<K, V>

    A lazy-evaluated iterator for objects with chainable operations

    支持链式操作的惰性计算对象迭代器,使用 Generator 实现单次遍历

    const result = new ObjectIterator({ a: 1, b: 2, c: 3 })
    .filter((k, v) => v > 1)
    .map((k, v) => [k.toUpperCase(), v * 2] as const)
    .toArray()
    // => [['B', 4], ['C', 6]]

    Type Parameters

    • K extends string = string

      The key type of entries. 条目的键类型

    • V = unknown

      The value type of entries. 条目的值类型

    Index

    Constructors

    • Creates a new ObjectIterator instance

      创建一个新的 ObjectIterator 实例

      Type Parameters

      • K extends string = string

        The key type of entries. 条目的键类型

      • V = unknown

        The value type of entries. 条目的值类型

      Parameters

      • obj: Record<K, V>

        The source object to iterate. 要迭代的源对象

      Returns ObjectIterator<K, V>

    Methods

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

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

      Returns Generator<[K, V]>

      An iterator for the processed entries. 处理后条目的迭代器

      // for...of loop
      for (const [key, value] of new ObjectIterator({ a: 1, b: 2 })) {
      console.log(key, value)
      }

      // spread operator
      const entries = [...new ObjectIterator({ a: 1, b: 2 })]

      // Array.from
      const arr = Array.from(new ObjectIterator({ a: 1, b: 2 }))
    • Filters entries based on a predicate function

      根据断言函数过滤条目

      Parameters

      • predicate: ObjectIteratorPredicate<K, V>

        The predicate function. 断言函数

      Returns ObjectIterator<K, V>

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

      new ObjectIterator({ a: 1, b: 2 })
      .filter((k, v) => v > 1)
      .toArray()
      // => [['b', 2]]
    • Transforms entries using a transform function

      使用转换函数转换条目

      Type Parameters

      • NK extends PropertyKey

        The new key type after transformation. 转换后的新键类型

      • NV

        The new value type after transformation. 转换后的新值类型

      Parameters

      • transform: ObjectIteratorTransform<K, V, NK, NV>

        The transform function. 转换函数

      Returns ObjectIterator<NK extends string ? NK : string, NV>

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

      new ObjectIterator({ a: 1, b: 2 })
      .map((k, v) => [k.toUpperCase(), v * 2] as const)
      .toArray()
      // => [['A', 2], ['B', 4]]
    • Executes all chained operations and returns the final result array

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

      Returns [K, V][]

      An array of [key, value] pairs. [key, value] 对数组

      new ObjectIterator({ a: 1, b: 2 }).toArray()
      // => [['a', 1], ['b', 2]]