So, I hvae following object in js:
var values= {
'first' : '42',
'last' : '43',
};
How do I get the key from the value?
For example, I have 42
and would like to get first
as the result.
Thanks!
So, I hvae following object in js:
var values= {
'first' : '42',
'last' : '43',
};
How do I get the key from the value?
For example, I have 42
and would like to get first
as the result.
Thanks!
You can use Object.keys() method
Object.keys(values).filter(function(key) {return values[key] === '42'})[0];
Simple for loop will help you:
var values= {
first: '42',
last: '43',
};
var val = '42', key;
for (key in values) {
if (values[key] == val) break;
}
document.write(key);