Open In App

Lodash _.hasIn() Method

Last Updated : 30 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

Syntax:

_.hasIn(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, or false.

Example 1: In this example, we are checking whether the given path is a direct or inherited property of the object by the use of the lodash _hasIn() method.

Javascript




// Requiring the lodash library 
const _ = require("lodash");
 
// Given object
let 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: In this example, we are checking whether the given path is a direct or inherited property of the object by the use of the lodash _hasIn() method.

Javascript




// Requiring the lodash library 
const _ = require("lodash");
 
// Given object
let 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

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads