[英]How do I transform a sequences of maps into a sequence of maps with selected keys?
I have a sequence of map like this 我有一系列这样的地图
({:a 1 :b 2 : c 4} {:a 3 :b 3 :d 4})
And I want to turn this into a sequence of more compact maps that just have the :a and :b keys, like this: 我想把它变成一个更紧凑的地图序列,它只有:a和:b键,如下所示:
({:a 1 :b 2} {:a 3 :b 3})
What's the most concise way to do this? 最简洁的方法是什么?
The built-in function select-keys is what you're looking for. 内置函数select-keys是您正在寻找的。
(let [in [{:a 1 :b 2 :c 4} {:a 3 :b 3 :d 4}]]
(map #(select-keys % [:a :b])
in))
A more generic solution would be to write a function that takes the keys you want to keep and returns a fn on maps. 更通用的解决方案是编写一个函数,该函数接受您想要保留的键并在映射上返回fn。 Then map it over the sequence of maps:
然后将其映射到地图序列上:
(defn keep-keys
[ks]
(fn [m] (select-keys m ks)))
(map (keep-keys [:a :b]) '({:a 1 :b 2 :c 4} {:a 3 :b 3 :d 4}))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.