UNPKG

@queso/pick

Version:

Creates an object composed of the picked object properties.

67 lines (48 loc) 3.66 kB
--- name: pick menu: Object route: /pick --- import { Link } from 'docz' import Editor from '../../src/components/Editor' # pick [![npm](https://nodei.co/npm/@queso/pick.svg?downloads=true)](https://nodei.co/npm/@queso/pick/) <svg width="144.3" height="20" viewBox="0 0 1443 200" xmlns="http://www.w3.org/2000/svg"> <g> <rect fill="#555" width="864" height="200"/> <rect fill="#08C" x="864" width="579" height="200"/> </g> <g fill="#fff" textAnchor="start" fontFamily="Verdana,DejaVu Sans,sans-serif" fontSize="110"> <text x="220" y="148" textLength="604" fill="#000" opacity="0.1">min + gzip</text> <text x="210" y="138" textLength="604">min + gzip</text> <text x="919" y="148" textLength="479" fill="#000" opacity="0.1">99 bytes</text> <text x="909" y="138" textLength="479">99 bytes</text> </g> <image x="40" y="35" width="130" height="132" href="https://queso.surge.sh/public/favicon.png"/> </svg> [source](https://github.com/jedmao/queso/blob/master/src/packages/pick/pick.ts) Creates an object composed of the picked `object` properties. ## Playground <Editor jsFunc="var%20pick%20%3D%20%28function%20%28%29%20%7B%0A%09%27use%20strict%27%3B%0A%0A%09/**%0A%09%20*%20Creates%20an%20object%20composed%20of%20the%20picked%20%60object%60%20properties.%0A%09%20*%20@param%20object%20The%20source%20object.%0A%09%20*%20@param%20paths%20The%20property%20paths%20to%20pick.%0A%09%20*%20@category%20Object%0A%09%20*%20@returns%20A%20new%20object%20composed%20of%20the%20picked%20%60object%60%20properties.%0A%09%20*%20@example%0A%09const%20obj%20%3D%20%7B%20a%3A%201%2C%20b%3A%202%2C%20c%3A%203%20%7D%0A%0A%09pick%28obj%2C%20%27a%27%2C%20%27c%27%29%0A%09//%20%3D%3E%20%7B%20a%3A%201%2C%20c%3A%203%20%7D%0A%09%20*/%0A%09function%20pick%28object%2C%20...paths%29%20%7B%0A%09%09return%20paths.reduce%28%28result%2C%20key%29%20%3D%3E%20%7B%0A%09%09%09if%20%28key%20in%20object%29%20%7B%0A%09%09%09%09result%5Bkey%5D%20%3D%20object%5Bkey%5D%3B%0A%09%09%09%7D%0A%09%09%09return%20result%0A%09%09%7D%2C%20%7B%7D%29%0A%09%7D%0A%0A%09return%20pick%3B%0A%0A%7D%28%29%29%3B%0A" tsFunc="/**%0A%20*%20Creates%20an%20object%20composed%20of%20the%20picked%20%60object%60%20properties.%0A%20*%20@param%20object%20The%20source%20object.%0A%20*%20@param%20paths%20The%20property%20paths%20to%20pick.%0A%20*%20@category%20Object%0A%20*%20@returns%20A%20new%20object%20composed%20of%20the%20picked%20%60object%60%20properties.%0A%20*%20@example%0Aconst%20obj%20%3D%20%7B%20a%3A%201%2C%20b%3A%202%2C%20c%3A%203%20%7D%0A%0Apick%28obj%2C%20%27a%27%2C%20%27c%27%29%0A//%20%3D%3E%20%7B%20a%3A%201%2C%20c%3A%203%20%7D%0A%20*/%0Afunction%20pick%3CT%20extends%20object%2C%20K%20extends%20keyof%20T%3E%28%0A%09object%3A%20T%2C%0A%09...paths%3A%20K%5B%5D%0A%29%20%7B%0A%09return%20paths.reduce%3CPartial%3CPick%3CT%2C%20K%3E%3E%3E%28%28result%2C%20key%29%20%3D%3E%20%7B%0A%09%09if%20%28key%20in%20object%29%20%7B%0A%09%09%09result%5Bkey%5D%20%3D%20object%5Bkey%5D%0A%09%09%7D%0A%09%09return%20result%0A%09%7D%2C%20%7B%7D%29%0A%7D%0A" value="const%20obj%20%3D%20%7B%20a%3A%201%2C%20b%3A%202%2C%20c%3A%203%20%7D%3B%0Apick%28obj%2C%20%27a%27%2C%20%27c%27%29%3B%0A" /> ### Result <div id="editor-result" /> ## Parameters | Name | Type | Description | | :------- | :---- | :-------------------------- | | `object` | `T` | The source object. | | `paths` | `K[]` | The property paths to pick. | ### Type parameters | Name | Constraint | | :--- | :--------- | | `T` | `object` | | `K` | `keyof T` | ## Returns A new object composed of the picked `object` properties. ### Return type ```ts Partial<Pick<T, K>> ```