ts-utils
  • Getting Started
  • Contributing
  • Testing
  • Functions
    • Array Methods
      • isArr
      • isArrEmpty
      • pushUniqueValue
      • pushOrUpdate
    • Date Methods
      • toNow
      • formatDate
      • fromNow
    • Number Methods
      • toFixed
      • parseNumber
      • parseFormat
      • addUnit
      • addSeparators
      • formatNumber
      • ensureNumber
      • numberEmptyState
    • Object Methods
      • parseJSON
      • deepClone
      • parseNumValues
      • removeEmptyKeys
      • sanitizeQuery
      • removeKeys
      • renameKey
      • renameKeys
      • sortByKeyLength
      • querylize
      • parseNumericObj
      • reserveKeys
      • hasKeysAndValues
    • String Methods
      • randomStr
      • toUpperCase
      • toLowerCase
      • toFullUrl
      • initials
      • generateUuid
Powered by GitBook
On this page
  • removeEmptyKeys(obj, shallow)
  • Arguments
  • Returns
  • Example
  • Usage
  • Notes

Was this helpful?

  1. Functions
  2. Object Methods

removeEmptyKeys

removeEmptyKeys(obj, shallow)

Recursively traverses an input object and removes keys with null, undefined, or empty string values. It provides the option to perform either a shallow or deep removal of empty keys based on the specified configuration.

Arguments

  • obj (Record<string, any>): The input object to remove empty keys from.

  • shallow (boolean, optional): Whether to perform a shallow or deep removal of empty keys. Defaults to false for deep removal.

Returns

  • Record<string, any>: The input object with empty keys removed.

Example

typescript
import { removeEmptyKeys } from 'check-engineering/ts-utils';

const data = {
  name: 'Alice',
  age: null,
  address: {
    street: '123 Main St',
    city: '',
    zip: undefined
  }
};

const cleanedData = removeEmptyKeys(data); // => { name: 'Alice', address: { street: '123 Main St' } }

Usage

typescript
import { removeEmptyKeys } from 'check-engineering/ts-utils';

const user = {
  name: 'Bob',
  email: '',
  profile: {
    bio: null,
    avatar: 'avatar.jpg'
  }
};

const sanitizedUser = removeEmptyKeys(user, true); // => { name: 'Bob', profile: { avatar: 'avatar.jpg' } }

Notes

  • The removeEmptyKeys function is designed to clean up objects by removing keys with null, undefined, or empty string values.

  • It supports both shallow and deep removal of empty keys, providing flexibility in data cleaning operations.

  • This function is useful for maintaining data integrity and reducing unnecessary keys in object structures.

PreviousparseNumValuesNextsanitizeQuery

Last updated 11 months ago

Was this helpful?