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.
// 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.
// 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