@bnidev/js-utils
    Preparing search index...

    Function compactArray

    • Filters out falsy values from an array, returning a new array with only truthy values. This includes removing null, undefined, false, 0, and empty strings.

      Type Parameters

      • T

      Parameters

      • arr: (undefined | null | false | "" | 0 | T)[]

        The array to compact.

      Returns T[]

      A new array containing only the truthy values from the original array.

      This utility is useful for cleaning up arrays where you want to remove unwanted or non-useful values, such as when processing user input or filtering results.

      // ES Module
      import { compactArray } from '@bnidev/js-utils'

      // CommonJS
      const { compactArray } = require('@bnidev/js-utils')
      // Compact an array with various falsy values
      const mixedArray = [1, null, 2, undefined, 0, "hello", false, ""]
      const compactedArray = compactArray(mixedArray)
      // compactedArray will be [1, 2, "hello"]