0
# lodash.has
1
2
The 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.
3
4
## Package Information
5
6
- **Package Name**: lodash.has
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install lodash.has`
10
11
## Core Imports
12
13
```javascript
14
var has = require('lodash.has');
15
```
16
17
For ES modules (using default import with CommonJS interop):
18
19
```javascript
20
import has from 'lodash.has';
21
```
22
23
## Basic Usage
24
25
```javascript
26
var has = require('lodash.has');
27
28
var object = { 'a': { 'b': 2 } };
29
var other = Object.create({ 'a': Object.create({ 'b': 2 }) });
30
31
// Check direct properties
32
has(object, 'a');
33
// => true
34
35
// Check nested properties with string path
36
has(object, 'a.b');
37
// => true
38
39
// Check nested properties with array path
40
has(object, ['a', 'b']);
41
// => true
42
43
// Inherited properties return false
44
has(other, 'a');
45
// => false
46
47
// Safe with null/undefined objects
48
has(null, 'a');
49
// => false
50
```
51
52
## Capabilities
53
54
### Property Existence Checking
55
56
Checks 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.
57
58
```javascript { .api }
59
/**
60
* Checks if `path` is a direct property of `object`.
61
*
62
* @static
63
* @since 0.1.0
64
* @category Object
65
* @param {Object} object The object to query.
66
* @param {Array|string} path The path to check.
67
* @returns {boolean} Returns `true` if `path` exists, else `false`.
68
* @example
69
*
70
* var object = { 'a': { 'b': 2 } };
71
* var other = Object.create({ 'a': Object.create({ 'b': 2 }) });
72
*
73
* has(object, 'a');
74
* // => true
75
*
76
* has(object, 'a.b');
77
* // => true
78
*
79
* has(object, ['a', 'b']);
80
* // => true
81
*
82
* has(other, 'a');
83
* // => false
84
*/
85
function has(object, path);
86
```
87
88
## Path Format Support
89
90
The `has` function supports multiple path formats:
91
92
**String paths:**
93
- Simple property: `'name'`
94
- Nested properties: `'user.profile.name'`
95
- Array indices: `'users[0].name'`
96
- Mixed notation: `'data.items[1].value'`
97
98
**Array paths:**
99
- Simple property: `['name']`
100
- Nested properties: `['user', 'profile', 'name']`
101
- Array indices: `['users', 0, 'name']`
102
- Mixed types: `['data', 'items', 1, 'value']`
103
104
## Behavior Details
105
106
### Direct Property Checking
107
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.
108
109
### Null Safety
110
The function safely handles `null` and `undefined` objects by returning `false` instead of throwing an error.
111
112
### Array-like Objects
113
For array-like objects (including actual arrays), the function can check both numeric indices and length properties:
114
115
```javascript
116
var array = [1, 2, 3];
117
118
has(array, '0'); // => true
119
has(array, 0); // => true
120
has(array, 'length'); // => true
121
has(array, '3'); // => false
122
```
123
124
### Symbol Properties
125
The function can check for Symbol properties when used as keys:
126
127
```javascript
128
var sym = Symbol('test');
129
var obj = { [sym]: 'value' };
130
131
has(obj, sym); // => true
132
```
133
134
## Error Handling
135
136
The `has` function does not throw errors under normal usage. It returns `false` for:
137
- `null` or `undefined` objects
138
- Non-existent paths
139
- Invalid path formats
140
141
Internal implementation may throw `TypeError` for invalid function arguments in the memoization helper, but this should not occur in normal usage of the `has` function.