Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80742 views
1
var baseFlatten = require('../internal/baseFlatten'),
2
bindCallback = require('../internal/bindCallback'),
3
pickByArray = require('../internal/pickByArray'),
4
pickByCallback = require('../internal/pickByCallback'),
5
restParam = require('../function/restParam');
6
7
/**
8
* Creates an object composed of the picked `object` properties. Property
9
* names may be specified as individual arguments or as arrays of property
10
* names. If `predicate` is provided it is invoked for each property of `object`
11
* picking the properties `predicate` returns truthy for. The predicate is
12
* bound to `thisArg` and invoked with three arguments: (value, key, object).
13
*
14
* @static
15
* @memberOf _
16
* @category Object
17
* @param {Object} object The source object.
18
* @param {Function|...(string|string[])} [predicate] The function invoked per
19
* iteration or property names to pick, specified as individual property
20
* names or arrays of property names.
21
* @param {*} [thisArg] The `this` binding of `predicate`.
22
* @returns {Object} Returns the new object.
23
* @example
24
*
25
* var object = { 'user': 'fred', 'age': 40 };
26
*
27
* _.pick(object, 'user');
28
* // => { 'user': 'fred' }
29
*
30
* _.pick(object, _.isString);
31
* // => { 'user': 'fred' }
32
*/
33
var pick = restParam(function(object, props) {
34
if (object == null) {
35
return {};
36
}
37
return typeof props[0] == 'function'
38
? pickByCallback(object, bindCallback(props[0], props[1], 3))
39
: pickByArray(object, baseFlatten(props));
40
});
41
42
module.exports = pick;
43
44