function getKeyByValue(object, value) {
  return Object.keys(object).find(key => object[key] === value);
}

ES6, no prototype mutations or external libraries.

Example,

function getKeyByValue(object, value) {
  return Object.keys(object).find(key => object[key] === value);
}


const map = {"first" : "1", "second" : "2"};
console.log(getKeyByValue(map,"2"));
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Answer from UncleLaz on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Object › keys
Object.keys() - JavaScript - MDN Web Docs
Object.keys() returns an array whose elements are strings corresponding to the enumerable string-keyed property names 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.
🌐
W3Schools
w3schools.com › jsref › jsref_object_keys.asp
JavaScript Object.keys() Method
Object.keys() returns the keys (properties) of any object type.
🌐
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
🌐
Mimo
mimo.org › glossary › javascript › object-keys-method
JavaScript Object.keys() method: Syntax, Usage, and Examples
The Object.keys() method in JavaScript returns an array of a given object’s own enumerable property names. It’s a useful tool for iterating over keys, checking what data an object holds, or transforming objects into arrays.
🌐
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 found directly upon object.
🌐
freeCodeCamp
freecodecamp.org › news › javascript-object-keys-tutorial-how-to-use-a-js-key-value-pair
JavaScript Object Keys Tutorial – How to Use a JS Key-Value Pair
November 11, 2020 - The key size has a value “small”. Each property is separated by a comma. All of the properties are wrapped in curly braces. This is the basic object syntax. But there are a few rules to keep in mind when creating JavaScript objects.
Find elsewhere
🌐
Career Karma
careerkarma.com › blog › javascript › javascript object.keys(): a guide
JavaScript Object.keys(): A Guide | Career Karma
December 1, 2023 - The Object.keys() method accepts one parameter: the name of the Object whose keys you want to retrieve. This method returns the names of all keys in the Object you have specified, stored as a JavaScript list.
🌐
Vultr Docs
docs.vultr.com › javascript › standard library › object › keys()
Get Object Keys - JavaScript
November 6, 2024 - The Object.keys() function in JavaScript offers a streamlined way to access the keys of an object, enriching the ability to handle and manipulate data dynamically. By exploring the various methods of leveraging this function, you can enhance ...
🌐
Programiz
programiz.com › javascript › library › object › keys
JavaScript Object.keys()
Online Python Online JavaScript ... Rust Online Scala Online Dart Online R Online Ruby ... The Object.keys() method returns an array of a given object's own enumerable property names....
🌐
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
🌐
Medium
medium.com › @conboys111 › how-do-object-keys-object-values-and-object-entries-differ-in-javascript-8d1c19901ecb
How Do Object.keys(), Object.values(), and Object.entries() ...
September 27, 2024 - With Object.keys() in hand, I approached my first exhibit. This map only showed the names of the treasures — nothing about their value, just the labels. Imagine walking through a hallway of artifacts, each with a tag underneath it. This map said, “Read the names of the treasures in the order they were added to the collection, but wait!
🌐
Dmitri Pavlutin
dmitripavlutin.com › access-object-keys-values-entries
How to Access Object's Keys, Values, and Entries in JavaScript
August 11, 2020 - Object.keys(hero) returns the list ['name', 'city'], which, as expected, are the keys of hero object.
🌐
Educative
educative.io › answers › how-to-get-keys-values-and-entries-in-javascript-object
How to get Keys, Values, and Entries in JavaScript Object?
The Object.entries(obj) method returns an array of the key-value pairs. These methods give developers different ways to work with objects in JavaScript, which facilitates data manipulation and other operations in applications.
🌐
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 - In JavaScript, getting the keys and values that comprise an object is very easy. You can retrieve each object’s keys, values, or both combined into an array. The examples below use the following object: const obj = { name: 'Daniel', age: 40, occupation: 'Engineer', level: 4 }; Getting an object’s keys The Object.keys() method returns […]
🌐
Zest
meetzest.com › home › blog › how to get keys of object javascript in 2026
How to Get Keys of Object Javascript in 2026 | Zest
April 2, 2026 - It gives you an array with all of an object’s own keys: strings, Symbols, enumerable, and non-enumerable. Nothing gets left behind. In modern JavaScript, it’s not uncommon for libraries or even your own code to use Symbols for special ...
🌐
Codecademy
codecademy.com › docs › javascript › objects › .keys()
JavaScript | Objects | .keys() | Codecademy
July 31, 2025 - The Object.keys() JavaScript method returns an array of a given object’s own enumerable string-keyed property names.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › objects: the basics
Objects
An object can be created with curly braces {…} with an optional list of properties. A property is a “key: value” pair, where key is a string (also called a “property name”), and value can be anything.