Open In App

Lodash _.has() Method

Improve
Improve
Like Article
Like
Save
Share
Report

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




// Requiring the lodash library 
const _ = require("lodash");
 
// Given object
let object = { 'a': { 'b': 2 } };
 
// Use of _.has method
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




// Requiring the lodash library 
const _ = require("lodash");
 
// Given object
let object = { 'a': { 'b': 2 } };
 
// Use of _.has method
console.log(_.has(object, 'a.b'));
console.log(_.has(object, ['a', 'b']));
console.log(_.has(object, ['a', 'b', 'c']));


Output:

true
true
false

Last Updated : 20 Oct, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads