forked from lodash/lodash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpickBy.js
29 lines (27 loc) · 852 Bytes
/
pickBy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import map from './map.js'
import basePickBy from './.internal/basePickBy.js'
import getAllKeysIn from './.internal/getAllKeysIn.js'
/**
* Creates an object composed of the `object` properties `predicate` returns
* truthy for. The predicate is invoked with two arguments: (value, key).
*
* @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
*
* const object = { 'a': 1, 'b': '2', 'c': 3 }
*
* pickBy(object, isNumber)
* // => { 'a': 1, 'c': 3 }
*/
function pickBy(object, predicate) {
if (object == null) {
return {}
}
const props = map(getAllKeysIn(object), (prop) => [prop])
return basePickBy(object, props, (value, path) => predicate(value, path[0]))
}
export default pickBy