0
var json={"america":"1234","india:"5678","britain":"789"}

This is a sample json object (there are thousands of key-value pairs in the actual json object). How do I retrieve the key from this object if I pass the value json["1234"]? The expected result is that I should be able to retrieve the key "america".

scrappedcola
  • 10,423
  • 1
  • 32
  • 43
user739115
  • 1,117
  • 5
  • 20
  • 41

1 Answers1

0

var json = {"america":"1234","india":"5678","britain":"789"};

function getValueByKey(obj, val) {
  // Looping on object keys
  return Object.keys(obj).filter(function (key) {
    return obj[key] === val
  })[0];
}

// For testing
console.log(getValueByKey(json, "1234")); // america

console.log(getValueByKey(json, "5678")); // india
Mohamed Abbas
  • 2,228
  • 1
  • 11
  • 19