The _.hasIn() method is used to check whether the path is a direct or inherited property of object or not. It returns true if path exists, else it returns false.
Syntax:
_.hasIn(object, path)
Parameters: This method accepts two parameters as mentioned above and described below:
- object: This parameter holds the object to query.
- path: This parameter holds the path to check. The path will be array or string.
Return Value: This method returns true if path exists, else false
Example 1:
Javascript
// Requiring the lodash library const _ = require( "lodash" ); // Given object var object = _.create({ 'a' : _.create({ 'b' : 2 }) }); // Use of _.hasIn method console.log(_.hasIn(object, 'a' )); console.log(_.hasIn(object, [ 'a' ])); console.log(_.hasIn(object, [ 'b' ])); |
Output:
true true false
Example 2:
Javascript
// Requiring the lodash library const _ = require( "lodash" ); // Given object var object = _.create({ 'a' : _.create({ 'b' : 2 }) }); // Use of _.hasIn method console.log(_.hasIn(object, 'a.b' )); console.log(_.hasIn(object, [ 'a' , 'b' ])); console.log(_.hasIn(object, [ 'a' , 'b' , 'c' ])); |
Output:
true true false