所以我有这个:
[{:a ["x" "y"], :b "foo"}
{:a ["x" "y" "z"], :b "bar"}]
想要这个:
[{:a "x", :b "foo"}
{:a "y", :b "foo"}
{:a "x", :b "bar"}
{:a "y", :b "bar"}
{:a "z", :b "bar"}]
我该怎么做?
答案 0 :(得分:5)
for
对于已知的嵌套级别非常好:
(for [x [{:a ["x" "y"], :b "foo"}
{:a ["x" "y" "z"], :b "bar"}]
a (:a x)]
(assoc x :a a))
答案 1 :(得分:3)
您可以使用mapcat
:
(def c [{:a ["x" "y"], :b "foo"}
{:a ["x" "y" "z"], :b "bar"}])
(mapcat (fn [{:keys [a] :as m}] (map #(assoc m :a %) a)) c)