UNPKG

@queso/pick-by

Version:

Creates an object composed of the object properties for which predicate returns truthy.

67 lines (48 loc) 4.18 kB
--- name: pick-by menu: Object route: /pick-by --- import { Link } from 'docz' import Editor from '../../src/components/Editor' # pick-by [![npm](https://nodei.co/npm/@queso/pick-by.svg?downloads=true)](https://nodei.co/npm/@queso/pick-by/) <svg width="151.3" height="20" viewBox="0 0 1513 200" xmlns="http://www.w3.org/2000/svg"> <g> <rect fill="#555" width="864" height="200"/> <rect fill="#08C" x="864" width="649" 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="549" fill="#000" opacity="0.1">124 bytes</text> <text x="909" y="138" textLength="549">124 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-by/pickBy.ts) Creates an object composed of the object properties for which predicate returns truthy. ## Playground <Editor jsFunc="var%20pickBy%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%20object%20properties%20for%20which%20predicate%20returns%20truthy.%0A%09%20*%20@category%20Object%0A%09%20*%20@param%20object%20The%20source%20object.%0A%09%20*%20@param%20predicate%20The%20function%20invoked%20per%20property.%0A%09%20*%20@returns%20The%20new%20object.%0A%09%20*%20@example%0A%09const%20obj%20%3D%20%7B%20a%3A%200%2C%20b%3A%20%27%27%2C%20c%3A%20true%2C%20d%3A%20%27hello%27%20%7D%0A%0A%09pickBy%28obj%29%0A%09//%20%3D%3E%20%7B%20c%3A%20true%2C%20d%3A%20%27hello%27%20%7D%0A%09%20*/%0A%09function%20pickBy%28object%2C%20predicate%20%3D%20v%20%3D%3E%20v%29%20%7B%0A%09%09return%20Object.keys%28object%29.reduce%28%28result%2C%20key%29%20%3D%3E%20%7B%0A%09%09%09const%20value%20%3D%20object%5Bkey%5D%3B%0A%09%09%09if%20%28predicate%28value%2C%20key%29%29%20%7B%0A%09%09%09%09result%5Bkey%5D%20%3D%20value%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%20pickBy%3B%0A%0A%7D%28%29%29%3B%0A" tsFunc="/**%0A%20*%20Creates%20an%20object%20composed%20of%20the%20object%20properties%20for%20which%20predicate%20returns%20truthy.%0A%20*%20@category%20Object%0A%20*%20@param%20object%20The%20source%20object.%0A%20*%20@param%20predicate%20The%20function%20invoked%20per%20property.%0A%20*%20@returns%20The%20new%20object.%0A%20*%20@example%0Aconst%20obj%20%3D%20%7B%20a%3A%200%2C%20b%3A%20%27%27%2C%20c%3A%20true%2C%20d%3A%20%27hello%27%20%7D%0A%0ApickBy%28obj%29%0A//%20%3D%3E%20%7B%20c%3A%20true%2C%20d%3A%20%27hello%27%20%7D%0A%20*/%0Afunction%20pickBy%3CT%20extends%20object%3E%28%0A%09object%3A%20T%2C%0A%09predicate%3A%20%3CK%20extends%20keyof%20T%3E%28value%3A%20T%5BK%5D%2C%20key%3A%20K%29%20%3D%3E%20any%20%3D%20v%20%3D%3E%20v%2C%0A%29%20%7B%0A%09return%20Object.keys%28object%29.reduce%3CPartial%3CT%3E%3E%28%28result%2C%20key%29%20%3D%3E%20%7B%0A%09%09const%20value%20%3D%20object%5Bkey%20as%20keyof%20T%5D%0A%09%09if%20%28predicate%28value%2C%20key%20as%20keyof%20T%29%29%20%7B%0A%09%09%09result%5Bkey%20as%20keyof%20T%5D%20%3D%20value%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%200%2C%20b%3A%20%27%27%2C%20c%3A%20true%2C%20d%3A%20%27hello%27%20%7D%3B%0ApickBy%28obj%29%3B%0A" /> ### Result <div id="editor-result" /> ## Parameters | Name | Type | Description | | :---------- | :------------------------------------------------ | :--------------------------------- | | `object` | `T` | The source object. | | `predicate` | `<K extends keyof T>(value: T[K], key: K) => any` | The function invoked per property. | ### Type parameters | Name | Constraint | | :--- | :--------- | | `T` | `object` | | `K` | `keyof T` | ## Returns The new object. ### Return type ```ts Partial<T> ```