Clojure get map key by value

前端 未结 3 1771
失恋的感觉
失恋的感觉 2020-12-31 11:58

I\'m a new clojure programmer.

Given...

{:foo \"bar\"}

Is there a way to retrieve the name of the key with a value \"bar\"?

3条回答
  •  萌比男神i
    2020-12-31 12:30

    user> (->> {:a "bar" :b "foo" :c "bar" :d "baz"} ; initial map
               (group-by val)   ; sorted into a new map based on value of each key
               (#(get % "bar")) ; extract the entries that had value "bar"
               (map key))     ; get the keys that had value bar
    (:a :c)
    

提交回复
热议问题