简体   繁体   English

更新Clojure中的地图向量中的值

[英]Update values in a vector of maps in clojure

I have a vector of maps with same keys: 我有一个具有相同键的地图向量:

(def items [{:id 1 :name "first item"}
            {:id 2 :name "second item"}])

I can uppercase the value of the :name key in the first map in the vector: 我可以在向量的第一张地图中将:name键的值大写:

(update-in items [0 :name] clojure.string/upper-case)
=> [{:id 1, :name "FIRST ITEM"} {:id 2, :name "second item"}]

How can I uppercase every :name key in every map? 如何在每个映射中将每个:name键大写? I expect this: 我期望这样:

[{:id 1, :name "FIRST ITEM"} {:id 2, :name "SECOND ITEM"}]

This should do it: 应该这样做:

(map #(update-in % [:name] clojure.string/upper-case) items)

The % sign stands in for each map in items in the function expression. %符号代表函数表达式中items每个映射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM