TS-Scribe - v0.6.2
    Preparing search index...

    Function partitionArray

    • Partitions an array into two arrays based on a predicate function.

      Elements for which the predicate returns true go into the first array, and elements for which it returns false go into the second array. The relative order of elements is preserved in both output arrays.

      Type Parameters

      • T

        The type of the array elements

      • S

        The narrowed type when a type predicate is provided

      Parameters

      • array: readonly T[]

        The array to partition

      • predicate: (value: T, index: number) => value is S

        A type predicate that narrows T to S

      Returns [S[], Exclude<T, S>[]]

      A tuple where the first array contains elements matching S, and the second contains the rest

      // Partition mixed types using a type predicate for type narrowing
      const mixed: Array<string | number> = [1, 'a', 2, 'b'];
      const [strings, numbers] = partitionArray(mixed, (x): x is string => typeof x === 'string');
      // strings: string[] => ['a', 'b']
      // numbers: number[] => [1, 2]
    • Partitions an array into two arrays based on a predicate function.

      Type Parameters

      • T

        The type of the array elements

      Parameters

      • array: readonly T[]

        The array to partition

      • predicate: (value: T, index: number) => boolean

        A function called with each element and its index

      Returns [T[], T[]]

      A tuple where the first array contains matching elements and the second contains the rest

      // Partition numbers into even and odd
      partitionArray([1, 2, 3, 4, 5], x => x % 2 === 0)
      // => [[2, 4], [1, 3, 5]]
      // Use the index in the predicate
      partitionArray(['a', 'b', 'c', 'd'], (_, i) => i % 2 === 0)
      // => [['a', 'c'], ['b', 'd']]