Lodash _.hasIn() Method
Last Updated :
30 Oct, 2023
Lodash _.hasIn() method is used to check whether the path is a direct or inherited property of the object or not. It returns true if the path exists, it returns false.
Syntax:
_.hasIn(object, path);
Parameters:
- object: This parameter holds the object to query.
- path: This parameter holds the path to check. The path will be an array or string.
Return Value:
This method returns true if the path exists, or false.
Example 1: In this example, we are checking whether the given path is a direct or inherited property of the object by the use of the lodash _hasIn() method.
Javascript
const _ = require( "lodash" );
let object = _.create({ 'a' : _.create({ 'b' : 2 }) });
console.log(_.hasIn(object, 'a' ));
console.log(_.hasIn(object, [ 'a' ]));
console.log(_.hasIn(object, [ 'b' ]));
|
Output:
true
true
false
Example 2: In this example, we are checking whether the given path is a direct or inherited property of the object by the use of the lodash _hasIn() method.
Javascript
const _ = require( "lodash" );
let object = _.create({ 'a' : _.create({ 'b' : 2 }) });
console.log(_.hasIn(object, 'a.b' ));
console.log(_.hasIn(object, [ 'a' , 'b' ]));
console.log(_.hasIn(object, [ 'a' , 'b' , 'c' ]));
|
Output:
true
true
false
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...