HomeLanguagesJavascriptUnderscore.js _.kv() Method

Underscore.js _.kv() Method

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




// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
var ob = { "gfg" : "neveropen" };
var val = _.kv( ob, "gfg");
  
console.log(val);


Output:

[ 'gfg', 'neveropen' ]

Example 2: if no key/value is found, this method returns undefined.

Javascript




// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
var ob = { "gfg" : "neveropen" };
var val = _.kv( ob, "geek");
  
console.log(val);


Output:

undefined
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

5 COMMENTS

Most Popular

Dominic
32688 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7057 POSTS0 COMMENTS
Nicole Veronica
12198 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12293 POSTS0 COMMENTS
Shaida Kate Naidoo
7182 POSTS0 COMMENTS
Ted Musemwa
7447 POSTS0 COMMENTS
Thapelo Manthata
7160 POSTS0 COMMENTS
Umr Jansen
7150 POSTS0 COMMENTS