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
  • removeKeys(obj, keys)
  • Arguments
  • Returns
  • Example
  • Usage
  • Notes

Was this helpful?

  1. Functions
  2. Object Methods

removeKeys

removeKeys(obj, keys)

Removes specified keys from an input object and returns a new object with the specified keys removed. It ensures that the original object remains unchanged while providing a clean copy with the specified keys removed.

Arguments

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

  • keys (string[]): The array of keys to remove from the object.

Returns

  • Record<string, any>: A new object with the specified keys removed.

Example

typescript
timport { removeKeys } from 'check-engineering/ts-utils';

const data = {
  id: 123,
  name: 'Alice',
  age: 30,
  city: 'New York'
};

const keysToRemove = ['id', 'city'];
const updatedData = removeKeys(data, keysToRemove); // ==> { name: 'Alice', age: 30 }

Usage

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

const user = {
  id: 456,
  username: 'john_doe',
  email: 'john@example.com',
  role: 'admin'
};

const keysToRemove = ['id', 'role'];
const updatedUser = removeKeys(user, keysToRemove); // ==> { username: 'john_doe', email: 'john@example.com' }

Notes

  • The removeKeys function provides a convenient way to remove specified keys from an object, creating a modified copy of the object without the specified keys.

  • It ensures data integrity by preserving the original object and returning a new object with the specified keys removed.

  • This function is useful for scenarios where specific keys need to be excluded from an object before further processing or data manipulation.

PrevioussanitizeQueryNextrenameKey

Last updated 11 months ago

Was this helpful?