Lodash _.has() Method

Lodash _.has() method is used to check whether the path is a direct property of the object or not. It returns true if the path exists, it returns false.
Syntax:
_.has(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. else it will return false.
Example 1: In this example, we are checking whether ‘a’ is present in the object or not by the use of the _.has() method.
Javascript
// Requiring the lodash library const _ = require("lodash");// Given objectlet 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: In this example, we are checking present of the passed value in the object or not by the use of the _.has() method.
Javascript
// Requiring the lodash library const _ = require("lodash");// Given objectlet 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
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, zambiatek 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!



