JavaScript Object.keys()
Example
const person = {
firstName: "John",
lastName: "Doe",
age: 50,
eyeColor: "blue"
};
// Get the Keys
const keys = Object.keys(person);
More Examples Below !
Description
The Object.keys()
method returns an array with the keys of an object.
The Object.keys()
method does not change the original object.
Related Methods:
Object.keys() returns the keys (properties) of any object type.
Object.values() returns the values of all object keys (properties).
Object.entries() returns the keys and values of any object types.
The methods above return an Iterable (enumerable array).
Iterables makes it simpler to use objects in loops and to convert objects into maps.
Syntax
Parameters
An iterable object.
Return Value
More Examples
Examples
This example list only the enumerable properties of an object:
const person = {
firstName: "John",
lastName: "Doe",
age: 50,
eyeColor: "blue"
};
// Change Property
Object.defineProperty(person, "age", {enumerable:false});
// Get the Keys
const keys = Object.keys(person);
Use Object.keys() on an array:
const keys = Object.keys(fruits);
Use Object.keys() on a string:
const keys = Object.keys(fruits);
Browser Support
Object.keys()
is an ECMAScript5 (ES5 2009) feature.
JavaScript 2009 is supported in all browsers since July 2013:
Chrome 23 |
IE/Edge 11 |
Firefox 21 |
Safari 6 |
Opera 15 |
Sep 2012 | Sep 2012 | Apr 2013 | Jul 2012 | Jul 2013 |