Search code examples
clojuremapsiteration

In clojure, how can I access and return a modified version of this collection


If I have a collection like this ({:A 1 :B 2 :Goal 5} {A:2 :B 4 :Goal 2}) is there a way I can iterate through the it and use assoc to change all :Goal to some other value, say 0? So basically given the collection above and the key :Goal the function returns ({:A 1 :B 2 :Goal 0} {A:2 :B 4 :Goal 0})

Any help is appreciated.

Thanks


Solution

  • (let [A '({:A 1 :B 2 :Goal 5} {:A 2 :B 4 :Goal 2})]
        (map #(assoc % :Goal 0) A))
    

    or, if you are keen to use specter:

    (let [A '({:A 1 :B 2 :Goal 5} {:A 2 :B 4 :Goal 2})]
        (setval [ALL :Goal] 0 A))