Object.pick
Object.pick
Function
Selects a field by key name and returns a new object.
Syntax
object.pick(keys) object.pick(key1, key2, ...)
Parameters
| Parameters | Type | Required | Default value | Description |
|---|---|---|---|---|
| keys | Array/String | No | [] | An array of keys to retain, or multiple key parameters. |
Return value
| Type | Description |
|---|---|
| Object | Returns a new object containing only the specified keys. |
Return object fields
The returned object contains only the fields hit by the passed key. The field value comes from the original object; the missed key will not be written to the returned object, and the read result is empty.
Example
user = {id: 1, name: 'BT', password: 'x'} result = user.pick(['id', 'name']) // Output: {"id":1,"name":"BT"} print result
Notes
- pick does not modify the original object.
- The returned objects are written in the order of the passed keys.