Skip to main content

compact

A function that removes false, 0, '', null, and undefined values from an array and returns a new array.


Code

🔗 View source code


Benchmark

  • hz: Operations per second
  • mean: Average response time (ms)
NamehzmeanPerformance
modern-kit/compact7,306,246.600.0004fastest
lodash/compact5,472,880.460.0006slowest
  • modern-kit/compact
    • 1.33x faster than lodash/compact

Interface

typescript
type Removable = false | 0 | '' | null | undefined;
type Retained<T> = Exclude<T, Removable>;

const compact: <T>(arr: T[] | readonly T[]) => Retained<T>[];

Usage

typescript
import { compact } from '@modern-kit/utils';

compact([0, 1, false, 2, '', 3, null, undefined, 4, NaN, 5]);
// [1, 2, 3, 4, 5]