How to get a key in a JavaScript object by its value?

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

Leave a Comment