The lodash method `_.has` exported as a module for checking if a path is a direct property of an object.
npx @tessl/cli install tessl/npm-lodash--has@4.5.0The lodash method _.has exported as a Node.js module. This package provides a standalone implementation of lodash's has method for checking if a path is a direct property of an object, supporting both string and array path notations for deep property access.
npm install lodash.hasvar has = require('lodash.has');For ES modules (using default import with CommonJS interop):
import has from 'lodash.has';var has = require('lodash.has');
var object = { 'a': { 'b': 2 } };
var other = Object.create({ 'a': Object.create({ 'b': 2 }) });
// Check direct properties
has(object, 'a');
// => true
// Check nested properties with string path
has(object, 'a.b');
// => true
// Check nested properties with array path
has(object, ['a', 'b']);
// => true
// Inherited properties return false
has(other, 'a');
// => false
// Safe with null/undefined objects
has(null, 'a');
// => falseChecks if a path is a direct property of an object. Supports both string paths (like 'a.b') and array paths (like ['a', 'b']) for deep property checking.
/**
* Checks if `path` is a direct property of `object`.
*
* @static
* @since 0.1.0
* @category Object
* @param {Object} object The object to query.
* @param {Array|string} path The path to check.
* @returns {boolean} Returns `true` if `path` exists, else `false`.
* @example
*
* var object = { 'a': { 'b': 2 } };
* var other = Object.create({ 'a': Object.create({ 'b': 2 }) });
*
* has(object, 'a');
* // => true
*
* has(object, 'a.b');
* // => true
*
* has(object, ['a', 'b']);
* // => true
*
* has(other, 'a');
* // => false
*/
function has(object, path);The has function supports multiple path formats:
String paths:
'name''user.profile.name''users[0].name''data.items[1].value'Array paths:
['name']['user', 'profile', 'name']['users', 0, 'name']['data', 'items', 1, 'value']The has method only checks for direct properties (own properties) of the object, not inherited properties from the prototype chain. This is different from the in operator which also checks inherited properties.
The function safely handles null and undefined objects by returning false instead of throwing an error.
For array-like objects (including actual arrays), the function can check both numeric indices and length properties:
var array = [1, 2, 3];
has(array, '0'); // => true
has(array, 0); // => true
has(array, 'length'); // => true
has(array, '3'); // => falseThe function can check for Symbol properties when used as keys:
var sym = Symbol('test');
var obj = { [sym]: 'value' };
has(obj, sym); // => trueThe has function does not throw errors under normal usage. It returns false for:
null or undefined objectsInternal implementation may throw TypeError for invalid function arguments in the memoization helper, but this should not occur in normal usage of the has function.