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

    Function partition

    • Splits an array into two groups based on a predicate function.

      The first group contains elements that satisfy the predicate function, and the second group contains elements that do not satisfy the predicate function.

      根据谓词函数将数组分成两组。

      第一组包含满足谓词函数的元素,第二组包含不满足谓词函数的元素。

      Type Parameters

      • T

      Parameters

      • array: readonly T[]

        The array to partition. 要分区的数组

      • predicate: (item: T, index: number, array: readonly T[]) => boolean

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

      Returns [T[], T[]]

      A tuple of two arrays: [elements that satisfy predicate, elements that don't]. 包含两个数组的元组:[满足谓词的元素, 不满足谓词的元素]

      O(n) time complexity - single pass through the array

      O(n) 时间复杂度 - 单次遍历数组

      partition([1, 2, 3, 4, 5], n => n % 2 === 0)
      // => [[2, 4], [1, 3, 5]]
      partition(['a', 'bb', 'ccc', 'd'], s => s.length > 1)
      // => [['bb', 'ccc'], ['a', 'd']]