Search code examples
javascriptobjectinverse

Look up object keys by value


Object = {"o":"s","e":"w"}

If I have this object, is there a way to perform reverse lookups on it? Something like:

Object.invert()["s"]
> "o"

Solution

  • You want to revert the key/value mapping.

    var test = {a: "b", c: "d"}
    var reverted = {}
    for(var key in test) {
        reverted[test[key]] = key
    }