Get key using value from an object in JavaScript?
As you already assumed you need to iterate over the object's attributes and check the value.
for(var key in c) {
if(c[key] === whatever) {
// do stuff with key
}
}
es6 find
method:
const getKey = (obj,val) => Object.keys(obj).find(key => obj[key] === val);
in your case
console.log(getKey(c,1)); // INDEX_SIZE_ERR
Underscore provides a more easy solution to this
You can get key using this code also
var errKey = _.invert(c)[errCode];
for e.x. if you use errCode = 3 as shown below
var errKey = _.invert(c)[3];
then
errKey
will be HIERARCHY_REQUEST_ERR