Home >Web Front-end >JS Tutorial >How Can I Easily Get an Array of JavaScript Object Keys?
One requires JavaScript object keys to be present as an array, leading to the usage of loops as follows:
var foo = { 'alpha': 'puffin', 'beta': 'beagle' }; var keys = []; for (var key in foo) { keys.push(key); }
However, a simpler approach is available.
With the Object.keys method, the array of object keys can be obtained concisely:
var foo = { 'alpha': 'puffin', 'beta': 'beagle' }; var keys = Object.keys(foo); console.log(keys) // ['alpha', 'beta']
The above is the detailed content of How Can I Easily Get an Array of JavaScript Object Keys?. For more information, please follow other related articles on the PHP Chinese website!