简体   繁体   中英

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

(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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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