reserveKeys
reserveKeys(obj, keys)
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
Notes
The
reserveKeysfunction 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.
Last updated
Was this helpful?