Files
lodash/pickBy.js
2017-01-09 17:38:33 -08:00

31 lines
866 B
JavaScript

import arrayMap from './_arrayMap.js';
import basePickBy from './_basePickBy.js';
import getAllKeysIn from './_getAllKeysIn.js';
/**
* Creates an object composed of the `object` properties `predicate` returns
* truthy for. The predicate is invoked with two arguments: (value, key).
*
* @static
* @since 4.0.0
* @category Object
* @param {Object} object The source object.
* @param {Function} predicate The function invoked per property.
* @returns {Object} Returns the new object.
* @example
*
* var object = { 'a': 1, 'b': '2', 'c': 3 };
*
* pickBy(object, isNumber);
* // => { 'a': 1, 'c': 3 }
*/
function pickBy(object, predicate) {
if (object == null) {
return {};
}
const props = arrayMap(getAllKeysIn(object), prop => [prop]);
return basePickBy(object, props, (value, path) => predicate(value, path[0]));
}
export default pickBy;