UNPKG

@queso/pick-by

Version:

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

22 lines (20 loc) 569 B
/** * Creates an object composed of the object properties for which predicate returns truthy. * @category Object * @param object The source object. * @param predicate The function invoked per property. * @returns The new object. * @example const obj = { a: 0, b: '', c: true, d: 'hello' } pickBy(obj) // => { c: true, d: 'hello' } */ export default function pickBy(object, predicate = v => v) { return Object.keys(object).reduce((result, key) => { const value = object[key] if (predicate(value, key)) { result[key] = value } return result }, {}) }