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

Answer from UncleLaz on Stack Overflow
🌐
Petermorgan
petermorgan.dev › blog › find-a-values-key-in-a-javascript-object
Find a Value's Key in a JavaScript Object - Peter Morgan - Website Developer
November 13, 2023 - I don’t know how many times I have accessed a JavaScript object’s value by its key or how I could arrive at a reasonable guess. What I do know is the number of times that I found the key from its corresponding value: once. This StackOverflow post is probably where you will end up if you ...
Discussions

javascript - Find object by key in array of object - Stack Overflow
Copyconst findIt = array.find( item => Object.keys(item)[0] === raceId); console.log(findIt) console.log(findIt['1004']) ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... Creating checkpoints by gaslighting a Postgres... Announcing Stack Overflow for... ... Help Shape the 2026 Developer Survey! ... In ... More on stackoverflow.com
🌐 stackoverflow.com
jquery - Find item by key in Javascript - Stack Overflow
meh["cars"] is returning undefined, since, as I understand, it cant find a description outside each object. I can do meh[0]["cars"] but it defeats the point as the position of cars might change. How do I access a value of something with their key please? More on stackoverflow.com
🌐 stackoverflow.com
Search the DOM for an object's key value and return the name of the object
Something like Object.keys(YOUR_OBJECT).find((key) => key === YOUR_KEY_VALUE) Make it recursive since you have nested objects. More on reddit.com
🌐 r/learnjavascript
7
2
April 25, 2024
javascript - Find element in Object Array by key value - Stack Overflow
What is the best structure solution for find element(Object) in Object Array by value of one of the keys. More on stackoverflow.com
🌐 stackoverflow.com
February 8, 2014
🌐
30 Seconds of Code
30secondsofcode.org › home › javascript › object › find matching keys
Find matching keys in a JavaScript object - 30 seconds of code
December 15, 2023 - const findKey = (obj, fn) => Object.keys(obj).find(key => fn(obj[key], key, obj)); findKey( { barney: { age: 36, active: true }, fred: { age: 40, active: false }, pebbles: { age: 1, active: true } }, x => x['active'] ); // 'barney' Recent versions of JavaScript also added Array.prototype.findLast() which can be used to find the last matching key.
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-get-a-key-in-a-javascript-object-by-its-value
How to get a key in a JavaScript object by its value ? | GeeksforGeeks
Example: In this example this function uses Object.keys() to get all keys, then uses find() to locate the key whose value matches 'Geeks'. It returns the first matching key (key1).
Published   September 13, 2024
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-get-object-key-by-value
Get an Object's Key by its Value using JavaScript | bobbyhadz
March 3, 2024 - Call the Object.keys() method to get an array of the object's keys. Use the find() method to find the key that corresponds to the value.
🌐
Coding Beauty
codingbeautydev.com › home › posts › how to get an object key by its value in javascript
How to Get an Object Key by Its Value in JavaScript
December 27, 2022 - The condition we specified only evaluates to true for a key in the array if its corresponding value is equal the value passed to the getObjectKey() function. If the find() method can’t find any element that satisfies the condition, it returns ...
🌐
Medium
medium.com › @louistrinh › javascript-object-queries-key-value-and-multi-condition-searches-1acac9c74da4
JavaScript Object Queries: Key, Value, and Multi-Condition Searches | by Louis Trinh | Medium
July 28, 2024 - const data = [ { id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }, { id: 3, name: 'Charlie' } ]; const targetKey = 'id'; const targetValue = 2; let foundObject; for (const obj of data) { if (obj[targetKey] === targetValue) { foundObject = obj; break; } } if (foundObject) { console.log('Found object:', foundObject); } else { console.log('Object not found'); } ... const foundObject = data.find(obj => obj[targetKey] === targetValue); if (foundObject) { console.log('Found object:', foundObject); } else { console.log('Object not found'); }
Find elsewhere
🌐
Medium
codingbeauty.medium.com › javascript-get-object-key-by-value-2bad7a22dcdb
How to Get an Object Key by Value in JavaScript | Medium
June 17, 2022 - The condition we specified only evaluates to true for a key in the array if its corresponding value is equal to the value passed to the getObjectKey() function. If the find() method can't find any element that satisfies the condition, it returns ...
🌐
freeCodeCamp
freecodecamp.org › news › how-to-check-if-an-object-has-a-key-in-javascript
JavaScript Key in Object – How to Check if an Object has a Key in JS
November 7, 2024 - // Using in operator 'key' in object // Using hasOwnProperty() method object.hasOwnProperty('key') You can use the JavaScript in operator to check if a specified property/key exists in an object.
🌐
Reddit
reddit.com › r/learnjavascript › search the dom for an object's key value and return the name of the object
r/learnjavascript on Reddit: Search the DOM for an object's key value and return the name of the object
April 25, 2024 -

I have been playing around with this for a bit and can't figure it out.

I want to query all of the objects in the DOM, looking through the sets, and when one of the objects of the set in question has a matching key value, I want to return the key value.

window.mysteryObj = {
    obj1:{
        abc: 1,
        def: 2,
    },
    obj2:{
        ghi: 3,
        jkl: 4,
    }
}

const keyValue = "ghi"

function findMysteryObj (key){
    //does some magic
    return objectName
}

findMysteryObj(keyValue); // should return "mysteryObj"

Is this possible? If someone could put me on the right track that would be amazing!

🌐
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 access a specific key-value pair in a JavaScript object, we can use Object.entries() to convert a JavaScript object into an array of key-value pairs and then filter this array to retrieve the value associated with a specific key. let obj ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › find
Array.prototype.find() - JavaScript - MDN Web Docs
The find() method reads the length property of this and then accesses each property whose key is a nonnegative integer less than length. ... const arrayLike = { length: 3, "-1": 0.1, // ignored by find() since -1 < 0 0: 2, 1: 7.3, 2: 4, }; console.log(Array.prototype.find.call(arrayLike, (x) ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-program-to-find-index-of-an-object-by-key-and-value-in-an-array
JavaScript Program to Find Index of an Object by Key and Value in an Array - GeeksforGeeks
July 23, 2025 - Example: In this example, we will find the index of an object by key and value in a JavaScript array using the for...of loop and Object.entries().
🌐
GitHub
gist.github.com › DaveAtDog › 11207429
JavaScript — Find key for value in an object
JavaScript — Find key for value in an object · Raw · findKey.js · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Top answer
1 of 3
2

I thought about restructuring and having object instead of array

Yes, that's fine.

BUT what if we want to keep the order?

I tend to use an extra array that contains the keys in the correct order, like

var order = [17, 34, 47];

To loop them, you'd use

for (var i=0; i<order.length; i++) {
    … someObject[order[i]] …
}
2 of 3
1

You should prefix the id to avoid naming collisions, like this:

var database = 
{
  'row_17' : {id:17, color:'black', width:50},
  'row_34' : {id:34, color:'red',   width:150},
  'row_49' : {id:49, color:'gree',  width:10}
};

At this point you can query the object with this code:

function retrieve(database, id)
{
  id = 'row_' + id;

  if (!database.hasOwnProperty(id))
  {
    return null;
  }

  return database[id];
}

The persist function would be:

function persist(database, obj)
{
  database['row_' + obj['id']] = obj;
}

If you need to keep an order you have first to understand what order you are talking about.

Of the id? Of the insert? Or of an arbitrary property?

There are solutions to all of these by filtering (extracting the items and putting them into a separate array that would be the query result or with additional fields or structures.

EDIT: how to keep insertion order

You'll need an array that keeps track of order:

database._ordered = [];

On insert, push the item there, too:

database._ordered.push(obj);

Now you can pick single items by key and all items ordered. There is no way you can have an order in an object, it's simply the wrong tool for that.

The row_ prefix is recommended to avoid naming collisions with methods and whatnot. An object should hold properties and methods, if you use it like a dictionary at least prevent interference by prefixing.

🌐
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 - How to Get an Object’s Keys and ... const obj = { name: 'Daniel', age: 40, occupation: 'Engineer', level: 4 }; The Object.keys() method returns an array of strings containing all of the object’s keys, sorted by order of ...
🌐
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.