[英]In clojure, how to find all the maps in a nested map that have some key
Let m
be a nested map. 令m
为嵌套地图。 How to find all the maps in m
that have a some key. 如何查找m
中所有具有某个键的地图。
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})
You can use the following: 您可以使用以下内容:
(def data {:a {:id 5}
:d {:id 58}
:x {:id 4 :c {:id 3 :d 4}}})
(defn recursive-filter [m f]
(filter #(and (map? %) (f %))
(tree-seq map? vals m)))
(recursive-filter data :id)
With inspiration from @Symfrog: 来自@Symfrog的灵感:
(defn recursive-filter [m k]
(filter #(and (map? %) (contains? % k)) (tree-seq map? vals m)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.