Array/Since
1.38.0compact
Removes falsy values from an array and returns a new array containing only truthy values.
compact(array):T[]Parameters
arrayT[]requiredThe array from which falsy values should be removed.
Returns
T[]A new array containing only the truthy values from the input array.
Examples
01
Basic usage
Remove falsy values from an array.
TypeScript
import { compact } from "@cyref/js";
const values = [0, 1, false, 2, "", 3, null, 4];
const result = compact(values);
console.log(result);
// [1, 2, 3, 4]02
Keep truthy values
Only truthy values are included in the returned array.
TypeScript
import { compact } from "@cyref/js";
const values = ["hello", "", "world", false, "CyrefJS"];
const result = compact(values);
console.log(result);
// ["hello", "world", "CyrefJS"]03
Empty result
If all values in the input array are falsy, the function returns an empty array.
TypeScript
import { compact } from "@cyref/js";
const values = [0, false, "", null, undefined];
const result = compact(values);
console.log(result);
// []Built with@cyref/js·Official documentation