HomeLanguagesJavascriptLodash _.hasIn() Method

Lodash _.hasIn() Method

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
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32738 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7120 POSTS0 COMMENTS
Nicole Veronica
12253 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12335 POSTS0 COMMENTS
Shaida Kate Naidoo
7228 POSTS0 COMMENTS
Ted Musemwa
7490 POSTS0 COMMENTS
Thapelo Manthata
7210 POSTS0 COMMENTS
Umr Jansen
7181 POSTS0 COMMENTS