@nevware21/ts-utils
    Preparing search index...

    Function arrPartition

    • The arrPartition() method splits an array into two arrays based on a predicate function. The first array contains elements for which the predicate returns true, the second contains the rest.

      Type Parameters

      • T

        Identifies the base type of array elements

      • E

        Identifies the narrowed type when using type guards

      Parameters

      • theArray: ArrayLike<T>

        The array or array-like object to partition

      • callbackFn: ArrPredicateCallbackFn<T, E> | ArrPredicateCallbackFn2<T>

        Function to test each element. Return true to include in first array, false for second

      • OptionalthisArg: any

        Optional value to use as this when executing callbackFn

      Returns [T[] | E[], T[]]

      A tuple of two arrays: [matching elements, non-matching elements]

      0.14.0

      arrPartition([1, 2, 3, 4, 5], x => x % 2 === 0);  // [[2, 4], [1, 3, 5]]
      arrPartition(["a", "bb", "ccc"], x => x.length > 1); // [["bb", "ccc"], ["a"]]

      // With type guard
      const mixed: (string | number)[] = [1, "a", 2, "b"];
      const [strings, numbers] = arrPartition(mixed, (x): x is string => typeof x === "string");
      // strings: string[], numbers: (string | number)[]

      // Array-like objects
      arrPartition({ length: 4, 0: 1, 1: 2, 2: 3, 3: 4 }, x => x > 2); // [[3, 4], [1, 2]]