Clojure - 更新向量内的哈希映射

时间:2018-03-28 18:46:28

标签: clojure hashmap

假设我有以下向量

[{:id "1" :type "type"}, {:id "2" :type "another-type"}]

我想写一个更新hashmap的函数,具体取决于它的id。

(defn update
  [vector id value]
  ....)

结果将是:

(update vector "1" "value")

[{:id "1" :type "type" :new-key ["value"]}, {:id "2" :type "another-type"}]

执行此更改的最常用方法是什么?

1 个答案:

答案 0 :(得分:0)

(mapv 
  (fn [m]
    (if (= "1" (:id m)) (assoc m :new-key ["value"]) m))
  vector)