Underscore.js _.kv() Method
Last Updated :
19 Aug, 2020
The _.kv() method returns the key/value pair for a given property in an object, returns undefined if not found.
Syntax:
_.kv( Object_name, property );
Parameters:
- Object_name: Object from which the key/value pair is to be searched.
- property: Given the property of which given key/value pair is to be searched.
Return Value: This method returns the key/value pair for a given property.
Note: This will not work in normal JavaScript because it requires the underscore.js contrib library to be installed.
Underscore.js contrib library can be installed using npm install underscore-contrib –save.
Example 1:
Javascript
var _ = require( 'underscore-contrib' );
var ob = { "gfg" : "GeeksforGeeks" };
var val = _.kv( ob, "gfg" );
console.log(val);
|
Output:
[ 'gfg', 'GeeksforGeeks' ]
Example 2: if no key/value is found, this method returns undefined.
Javascript
var _ = require( 'underscore-contrib' );
var ob = { "gfg" : "GeeksforGeeks" };
var val = _.kv( ob, "geek" );
console.log(val);
|
Output:
undefined
Share your thoughts in the comments
Please Login to comment...