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");
}