Thursday, December 26, 2024
Google search engine
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

Most Popular

Recent Comments