compact
Removes falsy values from an array. Creates a new array with all falsy values removed. Falsy values are: false, null, 0, "", undefined, and NaN. This is useful for cleaning arrays and ensuring only truthy values remain.
Installation
npx fragmen add array/compactSource Code
/**
* Removes falsy values from an array.
*
* Creates a new array with all falsy values removed. Falsy values are:
* false, null, 0, "", undefined, and NaN. This is useful for cleaning
* arrays and ensuring only truthy values remain.
*
* @tags pure, array-manipulation
* @param {T[]} array The array to remove falsy values from
* @returns {NonNullable<T>[]} A new array with falsy values removed
*
* @example
* ```typescript
* const mixed = [0, 1, false, 2, '', 3, null, 4, undefined, 5, NaN];
* const clean = compact(mixed);
* console.log(clean); // [1, 2, 3, 4, 5]
*
* const strings = ['hello', '', 'world', null, 'foo', undefined];
* const cleanStrings = compact(strings);
* console.log(cleanStrings); // ['hello', 'world', 'foo']
*
* const booleans = [true, false, true, null, false];
* const cleanBooleans = compact(booleans);
* console.log(cleanBooleans); // [true, true]
*
* // Empty array
* compact([]); // []
*
* // All falsy values
* compact([false, null, 0, '', undefined, NaN]); // []
* ```
*/
export function compact<T>(array: T[]): NonNullable<T>[] {
// Handle edge cases
if (!Array.isArray(array)) {
return [];
}
return array.filter((item): item is NonNullable<T> => Boolean(item));
}
Examples
const mixed = [0, 1, false, 2, '', 3, null, 4, undefined, 5, NaN];
const clean = compact(mixed);
console.log(clean); // [1, 2, 3, 4, 5]
const strings = ['hello', '', 'world', null, 'foo', undefined];
const cleanStrings = compact(strings);
console.log(cleanStrings); // ['hello', 'world', 'foo']
const booleans = [true, false, true, null, false];
const cleanBooleans = compact(booleans);
console.log(cleanBooleans); // [true, true]
// Empty array
compact([]); // []
// All falsy values
compact([false, null, 0, '', undefined, NaN]); // []
Related Utilities
chunk
arraySplits an array into chunks of a specified size. Creates a new array containing subarrays (chunks) of the original array, each with a maximum length of the specified size. The last chunk may contain fewer elements if the array length is not evenly divisible by the chunk size.
difference
arrayCreates an array of values from the first array that are not present in the other arrays. Returns a new array containing elements that exist in the first array but not in any of the subsequent arrays. The order of elements follows the order of the first array. Duplicates in the first array are preserved unless they appear in other arrays.
flatten
arrayFlattens nested arrays to a specified depth. Creates a new array with all sub-array elements concatenated into it recursively up to the specified depth. A depth of 1 flattens only the first level of nesting, while Infinity flattens all levels.
group-by
arrayGroups the elements of an array based on the result of a callback function. Creates an object where each key represents a group and the value is an array of items that belong to that group. The grouping is determined by the callback function which is applied to each element.
intersection
arrayFinds the intersection of two or more arrays. Returns a new array containing only the elements that are present in all provided arrays. The order of elements in the result follows the order of the first array. Duplicates are removed from the result.
sort-by
arraySorts an array of objects by a property or using a custom function. Creates a new sorted array without mutating the original. Can sort by a property name (for objects) or by using a custom iteratee function. Supports ascending and descending order.
Quick Actions
Tags
Parameters
arrayT[]The array to remove falsy values from
Returns
NonNullable<T>[]A new array with falsy values removed