compact

array

Creates an array with all falsy values removed. The values false, null, 0, "", undefined, and NaN are falsy.

Signature

compact<T>(array: T[]): NonNullable<T>[]

Parameters

NameTypeDescription
arrayT[]The array to compact

Returns

NonNullable<T>[] - Returns the new array of filtered values

Examples

Basic Usage

import { compact } from 'dashlite'

const result = compact([0, 1, false, 2, '', 3, null, undefined, NaN])
console.log(result)

Output:

[1, 2, 3]

With Objects

import { compact } from 'dashlite'

const result = compact([{ a: 1 }, null, { b: 2 }, undefined])
console.log(result)

Output:

[{ a: 1 }, { b: 2 }]

Available since version 1.0.0