Find Value of Specific Key in Nested Map

后端 未结 4 697
一向
一向 2021-02-08 09:40

In Clojure, how can I find the value of a key that may be deep in a nested map structure? For example:

(def m {:a {:b \"b\"
            :c \"c\"
            :d {         


        
4条回答
  •  北恋
    北恋 (楼主)
    2021-02-08 10:11

    If you know the nested path then use get-in.

    => (get-in m [:a :d :f])
    => "f"
    

    See here for details: https://clojuredocs.org/clojure.core/get-in

    If you don't know the path in your nested structure you could write a function that recurses through the nested map looking for the particular key in question and either returns its value when it finds the first one or returns all the values for :f in a seq.

提交回复
热议问题