[英]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? 如果我有一个这样的集合
({:A 1 :B 2 :Goal 5} {A:2 :B 4 :Goal 2})
,有一种方法可以迭代它,并使用assoc
将所有:Goal
更改为其他一些值,例如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})
因此,基本上给出了上面的集合以及键
:Goal
该函数返回({: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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.