Lodash _.has() Method
Last Updated :
20 Oct, 2023
Lodash _.has() method is used to check whether the path is a direct property of the object or not. It returns true if the path exists, it returns false.
Syntax:
_.has(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. else it will return false.
Example 1: In this example, we are checking whether ‘a’ is present in the object or not by the use of the _.has() method.
Javascript
const _ = require( "lodash" );
let object = { 'a' : { 'b' : 2 } };
console.log(_.has(object, 'a' ));
console.log(_.has(object, [ 'a' ]));
console.log(_.has(object, [ 'b' ]));
|
Output:
true
true
false
Example 2: In this example, we are checking present of the passed value in the object or not by the use of the _.has() method.
Javascript
const _ = require( "lodash" );
let object = { 'a' : { 'b' : 2 } };
console.log(_.has(object, 'a.b' ));
console.log(_.has(object, [ 'a' , 'b' ]));
console.log(_.has(object, [ 'a' , 'b' , 'c' ]));
|
Output:
true
true
false
Share your thoughts in the comments
Please Login to comment...