Array
compact

Removes all the falsy (opens in a new tab) values in the given array.

Immutable: This does not mutate the given array.

Syntax

import { compact } from '@opentf/std';
 
compact<T>(arr: T[] = []): Partial<T[]>;

Examples

compact([undefined]) //=>[]
 
compact([undefined, null]) //=>[]
 
compact([undefined, null, 0]) //=>[]
 
compact([undefined, null, 0, 1]) //=>[1]
 
compact([undefined, null, 0, 1, -0, 2]) //=>[1, 2]
 
compact([undefined, null, 0, 1, -0, 2, false, 3]) //=> [1, 2, 3];
 
compact([undefined, null, 0, 1, -0, 2, false, 3, true, NaN]) //=> [1, 2, 3, true]
 
compact(['apple', '', 'Mango']) //=>['apple', 'Mango']);

Try