In clojure, how to find all the cards in a nested card that have a key

advertisements

Let m be a nested map. How to find all the maps in m that have a some key.

For instance:

(def m {:a {:id 5}
        :d {:id 58}
        :x {:id 4 :c {:id 3 :d 4}}})

(recursive-filter m :id)

;; expected result:
> ({:id 5} {:id 58} {:id 4 :c {:id 3 :d 4}} {:id 3})


With inspiration from @Symfrog:

(defn recursive-filter [m k]
  (filter #(and (map? %) (contains? % k)) (tree-seq map? vals m)))