0
# Lodash Keys
1
2
Lodash Keys is a modularized version of lodash's `_.keys` functionality that creates an array of the own enumerable property names of an object. It provides a robust implementation with cross-browser compatibility and fallback support for environments without native Object.keys.
3
4
## Package Information
5
6
- **Package Name**: lodash.keys
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install lodash.keys`
10
11
## Core Imports
12
13
```javascript
14
var keys = require('lodash.keys');
15
```
16
17
For ES modules environments:
18
19
```javascript
20
import keys from 'lodash.keys';
21
```
22
23
## Basic Usage
24
25
```javascript
26
var keys = require('lodash.keys');
27
28
// Get keys from a plain object
29
var obj = { 'a': 1, 'b': 2, 'c': 3 };
30
console.log(keys(obj)); // => ['a', 'b', 'c'] (iteration order is not guaranteed)
31
32
// Works with constructor functions
33
function Foo() {
34
this.a = 1;
35
this.b = 2;
36
}
37
Foo.prototype.c = 3;
38
console.log(keys(new Foo)); // => ['a', 'b'] (excludes inherited properties)
39
40
// Handles string objects
41
console.log(keys('hi')); // => ['0', '1']
42
43
// Returns empty array for null/undefined
44
console.log(keys(null)); // => []
45
console.log(keys(undefined)); // => []
46
```
47
48
## Capabilities
49
50
### Object Keys Extraction
51
52
Creates an array of the own enumerable property names of an object, excluding inherited properties.
53
54
```javascript { .api }
55
/**
56
* Creates an array of the own enumerable property names of object.
57
* Non-object values are coerced to objects.
58
*
59
* @param {Object} object The object to query.
60
* @returns {Array} Returns the array of property names.
61
*/
62
function keys(object);
63
```
64
65
**Key Features:**
66
- Uses native `Object.keys` when available and appropriate
67
- Falls back to custom implementation for special cases (prototype objects, array-like objects)
68
- Handles non-object values by coercing them to objects
69
- Excludes inherited properties (own properties only)
70
- Cross-browser compatible with fallbacks for older environments
71
72
**Usage Examples:**
73
74
```javascript
75
var keys = require('lodash.keys');
76
77
// Plain objects
78
keys({ 'name': 'John', 'age': 30 }); // => ['name', 'age']
79
80
// Arrays (returns string indices)
81
keys(['a', 'b', 'c']); // => ['0', '1', '2']
82
83
// Strings (returns character indices)
84
keys('hello'); // => ['0', '1', '2', '3', '4']
85
86
// Constructor instances (excludes prototype properties)
87
function Person() {
88
this.name = 'John';
89
this.age = 30;
90
}
91
Person.prototype.species = 'human';
92
keys(new Person()); // => ['name', 'age']
93
94
// Array-like objects
95
var arrayLike = { '0': 'a', '1': 'b', 'length': 2 };
96
keys(arrayLike); // => ['0', '1', 'length']
97
98
// Edge cases
99
keys(null); // => []
100
keys(undefined); // => []
101
keys(42); // => [] (number coerced to Number object)
102
keys(true); // => [] (boolean coerced to Boolean object)
103
```
104
105
106
## Browser Compatibility
107
108
- Works in all modern browsers with native Object.keys support
109
- Includes fallback implementation for older browsers (IE8 and below)
110
- Handles various JavaScript engine quirks and edge cases
111
- Optimized for performance using native methods when available
112
113
## Dependencies
114
115
This package depends on other modularized lodash utilities:
116
117
- `lodash._getnative` (^3.0.0): For getting native method references
118
- `lodash.isarguments` (^3.0.0): For detecting arguments objects
119
- `lodash.isarray` (^3.0.0): For detecting arrays
120
121
These dependencies are automatically installed and provide specialized functionality for robust object property enumeration across different JavaScript environments.