The _.pick() function is used to return a copy of the object that filtered using the given key. This function accepts a predicate indicating which keys are picked from the object.
Syntax:
_.pick(object, *keys)
Parameters: This function accept two parameters as mentioned above and described below:
- object: This parameter holds the value of an object.
- keys: It is an optional parameter. It contains the key name that value need to be picked.
Return Value: It returns a copy of the object that filtered using the given key.
Example 1:
<!DOCTYPE html> < html > Â Â < head > Â Â Â Â < script type = "text/javascript" src = Â Â Â Â </ script > </ head > Â Â < body > Â Â Â Â < script type = "text/javascript" > Â Â Â Â Â Â Â Â Â Â var info = { Â Â Â Â Â Â Â Â Â Â Â Â Company: 'neveropen', Â Â Â Â Â Â Â Â Â Â Â Â Address: 'Noida', Â Â Â Â Â Â Â Â Â Â Â Â Contact: '+91 9876543210' Â Â Â Â Â Â Â Â }; Â Â Â Â Â Â Â Â Â Â console.log(_.pick(info, 'Company', 'Address')); Â Â Â Â </ script > </ body > Â Â </ html > |
Output:
Example 2:
<!DOCTYPE html> < html > Â Â < head > Â Â Â Â < script type = "text/javascript" src = Â Â Â Â </ script > </ head > Â Â < body > Â Â Â Â < script type = "text/javascript" > Â Â Â Â Â Â Â Â Â Â var info = { Â Â Â Â Â Â Â Â Â Â Â Â key1: 10, Â Â Â Â Â Â Â Â Â Â Â Â key2: 20, Â Â Â Â Â Â Â Â Â Â Â Â key3: 30, Â Â Â Â Â Â Â Â Â Â Â Â key4: 40, Â Â Â Â Â Â Â Â Â Â Â Â key5: 50 Â Â Â Â Â Â Â Â }; Â Â Â Â Â Â Â Â Â Â console.log(_.pick(info, function (value, key, info) { Â Â Â Â Â Â Â Â Â Â Â Â return value == 10 || value == 50; Â Â Â Â Â Â Â Â })); Â Â Â Â </ script > </ body > Â Â </ html > |
Output: