keys()
keys<
T>(object): keyofT[]
keys(
object): []
keys(
object):string[]
Creates an array of the own enumerable property names of an object.
DEPRECATED
Use Object.keys() directly instead.
Type Parametersβ
T: T extends objectβ
The type of the object.
Parametersβ
Overload 1:
object: Tβ
The object to query.
Overload 2:
object: null | undefinedβ
The object to query.
Overload 3:
object: unknownβ
The object to query.
Returns: keyof T[]β
An array of property names.
See Alsoβ
Sinceβ
2.0.0
Also known asβ
keys (Lodash, es-toolkit, Remeda, Radashi, Ramda, Effect) Β· objectKeys (Antfu) Β· β (Modern Dash)
Exampleβ
const obj = { a: 1, b: 2, c: 3 };
// β Deprecated approach
const objKeys = keys(obj);
console.log(objKeys); // ['a', 'b', 'c']
// β
Recommended approach
const nativeKeys = Object.keys(obj);
console.log(nativeKeys); // ['a', 'b', 'c']
How it works?β
Creates an array of own enumerable property names.
Deprecated: Use Object.keys() directly (ES5).
Native Equivalentβ
// β keys(obj)
// β
Object.keys(obj)
Use Casesβ
Get object keys πβ
Get all property names.
Object.keys(obj); // => ["a", "b", "c"]
Object.values(obj); // => [1, 2, 3]
Object.entries(obj); // => [["a", 1], ["b", 2], ["c", 3]]
Iterate propertiesβ
Loop over object properties.
for (const [key, value] of Object.entries(obj)) {
console.log(key, value);
}
Transform objectβ
Transform using entries.
Object.fromEntries(
Object.entries(obj).map(([k, v]) => [k, v * 2])
);
Count object properties for validationβ
Check the number of properties in an object to enforce limits. Useful for API payload validation, configuration checks, and form field counting.
const payload = { name: "Alice", email: "alice@example.com", role: "admin" };
const fieldCount = keys(payload).length;
// => 3
if (fieldCount > 50) {
throw new Error("Payload too large: max 50 fields allowed");
}