简体   繁体   English

在Clojure中,如何在嵌套地图中查找具有某些键的所有地图

[英]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.

相关问题 给定Clojure中的一系列映射,如何过滤键的值>(某个值)? - Given a sequence of maps in Clojure, how to filter for value of a key > (some value)? 为什么使用`std::map::find` 来检查地图是否有密钥? - Why use `std::map::find` for checking if maps have a key? Clojure 嵌套映射。 从值访问父键 - Clojure nested maps. Access parent-key from value 在Clojure中有没有找到地图中匹配键和值的惯用方法? - Is there an idiomatic way to find matching key and value in map in Clojure? 如何使用clojure中的函数替换嵌套映射中的多个值? - How to replace multiple values within a nested map using a function in clojure? Clojure:如何将函数应用于嵌套映射中的每个值并进行更新? - Clojure: How to apply a function to every value in a nested map and update? 在Clojure中,如何正确更新嵌套地图? - In Clojure, How do I update a nested map correctly? 如何在clojure中获取地图的嵌套键? - How can I get the nested keys of a map in clojure? 如何在映射中检索其值包含clojure中的特定子字符串的键? - how to retrieve a key in a map whose value contains a particular substring in clojure? Clojure:如何使用字符串键从地图获取 - Clojure: how to get from a map using a string key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM