Underscore.js _.hasPath() Method
Last Updated :
19 Aug, 2020
The _.hasPath() method returns a boolean indicating whether there is a property at the path described by the keys given. Keys may be given as an array or as a dot-separated string.
Syntax:
_.hasPath( Object_name, key_string|array );
Parameters:
- Object_name: Object from which the value is to be searched.
- key_string | array: Given dot-separated string or array to be searched in the given object.
Return Value: This method returns a Boolean.
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 : "Computer Science Portal for Geeks"
}
};
var val = _.hasPath( ob, [ 'gfg' , 'GeeksforGeeks' ] );
console.log(val);
|
Output:
true
Example 2:
Javascript
var _ = require( 'underscore-contrib' );
var ob = { gfg : {
GeeksforGeeks : "Computer Science Portal for Geeks"
}
};
var val = _.hasPath( ob, 'gfg.GeeksforGeeks' );
console.log(val);
|
Output:
true
Example 3:
Javascript
var _ = require( 'underscore-contrib' );
var ob = { gfg : {
GeeksforGeeks : "Computer Science Portal for Geeks"
}
};
var val = _.hasPath( ob, 'gfg.Geeks' );
console.log(val);
|
Output:
false
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...