Skip to content
Related Articles

Related Articles

Improve Article

Lodash _.has() Method

  • Last Updated : 09 Sep, 2020

The _.has() method is used to check whether the path is a direct property of object or not. It returns true if path exists, else it returns false.

Syntax:

_.has(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 = { '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:  

Javascript




// Requiring the lodash library  
const _ = require("lodash");  
  
// Given object
var 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
My Personal Notes arrow_drop_up
Recommended Articles
Page :