Use Object.keys:

var foo = {
  'alpha': 'puffin',
  'beta': 'beagle'
};

var keys = Object.keys(foo);
console.log(keys) // ['alpha', 'beta'] 
// (or maybe some other order, keys are unordered).

This is an ES5 feature. This means it works in all modern browsers but will not work in legacy browsers.

The ES5-shim has a implementation of Object.keys you can steal

Answer from Raynos on Stack Overflow
🌐
W3Schools
w3schools.com › jsref › jsref_object_keys.asp
JavaScript Object.keys() Method
The Object.keys() method returns an array with the keys of an object.
Discussions

javascript - Get array of object's keys - Stack Overflow
I would like to get the keys of a JavaScript object as an array, either in jQuery or pure JavaScript. Is there a less verbose way than this? var foo = { 'alpha' : 'puffin', 'beta' : 'beagle' }; var More on stackoverflow.com
🌐 stackoverflow.com
Generate an Array of All Object Keys with Object.keys()
Tell us what’s happening: I have a good understanding on how to use Object.keys() with 1 object; however, I want to understand how this would work in a nested array with 2 or more objects. **Your code so far** let u… More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
1
0
August 11, 2022
Object.keys
obj is an object with the keys "username1", "username2", and "username3", and the respective values of "ben", "kevin", and "deku". The operation Object.keys(obj) creates and returns a new array object that has all the keys of obj. That array would look like ["username1", "username2", "username3"] where "username1" has an index of 0, "username2" has an index of 1, and "username3" has an index of 2. When you call forEach on this array, the first two arguments in the callback function is going to be the value of the element in that iteration of the array ("username1", "username2", or "username3") and the index of the element in that iteration (0, 1, and 2, respectively). Ultimately you're working with two objects, obj which has key-value pairs, and the keys array from Object.keys(obj) which has indexed values matching the keys of obj. More on reddit.com
🌐 r/learnjavascript
21
1
June 26, 2024
Should you build a hash table using an array or an object?
It depends on whether or not the implementation is using the array features of the table. If so, it may be necessary (or at least, convenient) to have it as an array. If not, an object is fine. Off the top of my head I wouldn't think the table would need to be an array, but it could depend on what they're doing. More on reddit.com
🌐 r/learnjavascript
7
2
January 18, 2024
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › keys
Array.prototype.keys() - JavaScript - MDN Web Docs
July 20, 2025 - const arr = ["a", , "c"]; const sparseKeys = Object.keys(arr); const denseKeys = [...arr.keys()]; console.log(sparseKeys); // ['0', '2'] console.log(denseKeys); // [0, 1, 2] The keys() method reads the length property of this and then yields all integer indices between 0 and length - 1. No index access actually happens. ... const arrayLike = { length: 3, }; for (const entry of Array.prototype.keys.call(arrayLike)) { console.log(entry); } // 0 // 1 // 2
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Object › entries
Object.entries() - JavaScript - MDN Web Docs
Each key-value pair is an array with two elements: the first element is the property key (which is always a string), and the second element is the property value. Object.entries() returns an array whose elements are arrays corresponding to the enumerable string-keyed property key-value pairs ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-object-keys-method
JavaScript Object keys() Method - GeeksforGeeks
The Object.keys() method in JavaScript is used to retrieve an array of the enumerable property names of an object.
Published   July 12, 2024
Find elsewhere
🌐
Mimo
mimo.org › glossary › javascript › object-keys-method
JavaScript Object.keys() method: Syntax, Usage, and Examples
Object.keys() returns an array of a given object's own property names—ideal for loops, filtering, transforming, or counting object keys.
🌐
Reddit
reddit.com › r/learnjavascript › object.keys
r/learnjavascript on Reddit: Object.keys
June 26, 2024 -

Do you use Object.keys often?

Can you explain what's going on in this code what would be the key, index.

At first I was still thing they was object not arrays because that's what they looked like. I know the key was username1/2/3/ and the value is ben/kevin/deku but they don't have index and removing index from the parameters changed nothing, but arrays have index but not keys. from the cosole.log to get the key we console.log key, and to get the value its obj[key] but why?

I tried console.log(Object.keys(obj)) but that mad it more confusing because now the names(values) where gone and it was just an array of the username(keys)

let obj ={
    username1: "ben",
    username2: "kevin",
    username3: "deku"
}
Object.keys(obj).forEach((key, index) =>{
    console.log(key, obj[key]);
})
// username1 ben
// username2 kevin
// username3 deku



let obj ={
    username1: "ben",
    username2: "kevin",
    username3: "deku"
}
Object.keys(obj).forEach((key) =>{
    console.log(key, obj[key]);
})
// username1 ben
// username2 kevin
// username3 deku



let obj ={
    username1: "ben",
    username2: "kevin",
    username3: "deku"
}
Object.keys(obj).forEach((key, index) =>{
    console.log(obj[key]);
})
// ben
// kevin
// deku



let obj ={
    username1: "ben",
    username2: "kevin",
    username3: "deku"
}
Object.keys(obj).forEach((key) =>{
    console.log(key);
})
// username1
// username2
// username3
🌐
Tabnine
tabnine.com › home › how to get an object’s keys and values in javascript
How to Get an Object’s Keys and Values in JavaScript - Tabnine
July 25, 2024 - The Object.keys() method returns an array of strings containing all of the object’s keys, sorted by order of appearance:
🌐
Educative
educative.io › answers › how-to-get-keys-values-and-entries-in-javascript-object
How to get Keys, Values, and Entries in JavaScript Object?
To get the JavaScript keys, values, and entries of a JavaScript object data type, we use the following method: The Object.keys(obj) method returns all the keys of the object as an array.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Object › values
Object.values() - JavaScript - MDN Web Docs
Object.values() returns an array whose elements are values of enumerable string-keyed properties found directly upon object. This is the same as iterating with a for...in loop, except that a for...in loop enumerates properties in the prototype chain as well.
🌐
Programiz
programiz.com › javascript › library › object › keys
JavaScript Object.keys()
console.log(Object.keys(arr)); // ['0', '1', '2'] // array-like objects const obj = { 65: "A", 66: "B", 67: "C" };
🌐
W3Schools
w3schools.com › jsref › jsref_keys.asp
JavaScript Array keys() Method
❮ Previous JavaScript Array Reference Next ❯ · Create an Array Iterator object, containing the keys of the array: // Create an Array const fruits = ["Banana", "Orange", "Apple", "Mango"]; // Create an Iterable const list = fruits.keys(); // List the Keys let text = ""; for (let x of list) { text += x + "<br>"; } Try it Yourself » ·
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-convert-array-values-to-object-keys
Convert an Array's Values to Object Keys in JavaScript | bobbyhadz
March 4, 2024 - Declare a new variable and initialize it to an empty object. Use the forEach() method to iterate over the array. On each iteration, assign the array's element as a key in the object.
🌐
Medium
medium.com › @lainakarosic › using-object-keys-to-turn-javascript-objects-into-array-f248dc716321
Using Object.keys() to Turn Javascript Objects into Array | by Laina Karosic | Medium
November 25, 2019 - Voila! Here we have a nice return of the keys of our newly created array. Object.keys() essentially extracts all the keys and returns a new array.
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › Object › keys
JavaScript Object keys() - Get Object Keys | Vultr Docs
November 6, 2024 - This code snippet extracts the keys name, age, and occupation from the person object and stores them in the keys array, outputting ['name', 'age', 'occupation'].
🌐
GeeksforGeeks
geeksforgeeks.org › object-keys-javascript
Object.keys( ) In JavaScript - GeeksforGeeks
November 30, 2021 - Object.keys() is used for returning enumerable properties of an array like object with random key ordering.