You can use clojure.walk/postwalk
to replace nested values that exist in maps and vectors.
(defn replace-values [data target-key new-value]
(clojure.walk/postwalk
(fn [x]
(cond
(map? x) (into {} (map (fn [[k v]] [k (if (= k target-key) new-value v)])) x)
(vector? x) (vec (map #(replace-values % target-key new-value) x))
:else x)) data))
(def data {:cart {:something 0 :fruit {:apples 1 :bananas 2 :something 1}} :vegetables [{:eggplants 10 :potatoes 3 :something 0}]})
(replace-values data :something 42)
;; => {:cart {:something 42, :fruit {:apples 1, :bananas 2, :something 42}}, :vegetables [{:eggplants 10, :potatoes 3, :something 42}]}
Top comments (0)