6

什么是对复杂嵌套结构的嵌套字段施加条件的最佳方法,例如......

{
    :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”,那么得到如下图

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

1 回答 1

3
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"}}}])
于 2012-04-12T18:08:23.110 回答