Function: objClean()
objClean<
T,Key>(obj,keys,ignoreIfNotExist):Record<Key,T[Key]>
Defined in: packages/core/src/obj/objClean.ts:27
Constructs a new object with only the supplied property names (keys) and their respective values
Type Parameters
T
T extends Record<PropertyKey, unknown>
Key
Key extends string | number | symbol = keyof T
Parameters
obj
T
keys
Key[]
property names to keep
ignoreIfNotExist
boolean = true
(optional) if truthy, will ignore non-existent keys. Default: true
Returns
Record<Key, T[Key]>
Example
Create a new object from an existing object by only extracting specified keys and relevant values
javascript
import { objClean } from '@superutils/core'
const obj = {
a: 1,
b: 2,
c: 3,
d: 4
}
console.log(objClean(obj, [ 'a', 'b']))
// { a: 1, b: 2 }