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

Was this helpful?

  1. Functions
  2. Object Methods

reserveKeys

reserveKeys(obj, keys)

Creates a new object containing only the specified keys from the original input object. It filters out keys that are not included in the specified array and returns a new object with the reserved keys.

Arguments

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

  • keys (string[]): The array of keys to reserve in the new object.

Returns

  • Record<string, any>: A new object containing only the specified keys.

Example

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

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

const reservedKeys = reserveKeys(data, ['name', 'city']); // ==> { name: 'Alice', city: 'New York' }

Usage

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

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

const reservedUserKeys = reserveKeys(user, ['username', 'email']); // ==> { username: 'john_doe', email: 'john@example.com' }

Notes

  • The reserveKeys function creates a new object containing only the specified keys from the input object.

  • It preserves the original object and returns a new object with the selected key-value pairs.

  • This function is useful for creating a subset of an object containing only the necessary keys for specific use cases.

PreviousparseNumericObjNexthasKeysAndValues

Last updated 11 months ago

Was this helpful?