过滤具有复杂嵌套结构的地图 [英] Filter a map with complex nested structure

查看:121
本文介绍了过滤具有复杂嵌套结构的地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对复杂嵌套结构的嵌套字段强加一个条件的最佳方法是...

What would be a best way to impose a condition on the nested fields of complex nested structure like...

{
    :aa {:a "a_val",:b "b_val"},
    :qq {:abc 
            {
                :x1 {:x "abc",:u "ee"},
                :x2 {:y "abc",:i "ee"},
                :x3 {:x "abc",:i "ee"}
        }   
        },
    :ww {:xyz {
                :y1 {:x "abc",:u "ee"},
                :y2 {:y "abc",:i "0"},
                :y3 {:x "abc",:i "ee"}
              } 
        }
}

我想检查i部分是否存在,并且在aa,qq和ww的每一个中都有值0,并且取决于排除任何操作)对aa,qq和ww。例如,如果ww在那个位置有i=0,那么得到如下的地图

I want to check whether the "i" part exist and has value "0" in each of aa,qq and ww and depending upon that exclude(or perform any operation) on aa,qq and ww. For example if "ww" has "i"="0" at that position then get a map like below

{
    :ww {:xyz {
            :y1 {:x "abc",:u "ee"},
            :y2 {:y "abc",:i "0"},
            :y3 {:x "abc",:i "ee"}
            }   
        }
}


推荐答案

user> (defn vvals [m] (when (map? m) (vals m)))
'user/vvals
user> (filter #(some #{"0"} (for [v (vvals (val %)), v (vvals v)] (:i v))) xx)
([:ww {:xyz {:y3 {:x "abc", :i "ee"}, :y2 {:y "abc", :i "0"}, :y1 {:x "abc", :u "ee"}}}])

这篇关于过滤具有复杂嵌套结构的地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆