Say I have a JavaScript object like so:
var myObject = {
key1: "value1",
key2: "value2",
key3: "value3"
}
We all know I can get value1
by using myObject ['key1']
, but is there any way to do the reverse, like myObject ["value1"]
returning 'key1'
?
I understand that I can use
var values = Object.values (myObject);
var index = values.indexOf ("value1");
var keys = Object.keys (myObject);
keys [index];
to do this, but that's just long-winded. I also understand that this would be even more cumbersome when multiple keys have the same value. I'm just wondering if there is some other way to make doing something like this easier.