我有一个包含哈希图的Clojure向量。这些哈希映射本身不与任何密钥相关联。
但是这些哈希映射的所有都有一个共同的密钥 - 让我们说"ckey"
。我想提取"ckey"
的值为"test"
的那些哈希图。
数据实际上是一个解析过的json。这样的事情: -
testdata :{
sample1 : "samplevalue1"
sample2 : "samplevalue2"
sample3 : {innersample3 : "innersamplevalue3"}
sample4 : "samplevalue4"
sample5 : [{moresample []
link {innersample5 : "innersamplevalue5"}
ckeydata [{ckey:"test" something: somethingValue something2: somethingValue2}]}
{moresample []
link {innersample5 : "innersamplevalue5"}
ckeydata [{ckey:"test1"something: somethingValue something2: somethingValue2}]}
{moresample []
link {innersample5 : "innersamplevalue5"}
ckeydata [ {ckey:"test2" something: somethingValue something2: somethingValue2}]}
{moresample []
link {innersample5 : "innersamplevalue5"}
ckeydata [ {ckey:"test" something: somethingValue something2: somethingValue2}]}]}
因此,在这种情况下,我想从sample5
中提取散列图,其"ckeydata"
键的向量包含"ckey"
值为"test"
的散列映射。
Clojure格式的数据:
testdata {
:sample1 "samplevalue1"
:sample2 "samplevalue2"
:sample3 {:innersample3 "innersamplevalue3"}
:sample4 "samplevalue4"
:sample5 [{:moresample []
:link {innersample5 "innersamplevalue5"}
:ckeydata [{:ckey:"test" :something: somethingValue something2 somethingValue2}]}
{:moresample []
:link {innersample5 "innersamplevalue5"}
:ckeydata [{:ckey "test1" :something "somethingValue" :something2 "somethingValue2"}]}
{:moresample []
:link {:innersample5 "innersamplevalue5"}
:ckeydata [ {:ckey "test2" :something "somethingValue" :something2 "somethingValue2"}]}
{:moresample []
:link {innersample5 : "innersamplevalue5"}
:ckeydata [ {:ckey "test" :something "somethingValue" :something2 "somethingValue2"}]}]}
注意:我不想使用许多过滤器并继续循环代码。
答案 0 :(得分:3)
这应该有效:
(filter
(fn [{:keys [ckeydata]}]
(some (comp #{"test"} :ckey) ckeydata))
(:sample5 testdata))
这是一个相当密集的版本:
(filter
(fn [sample-map]
(some
(fn [element]
(= (:ckey element) "test"))
(:ckeydata sample-map)))
(:sample5 testdata))
如果:ckeydata
始终只包含一个元素,请考虑:
(filter
(comp #{"test"} :ckey first :ckeydata)
(:sample5 testdata))