Reputation: 948
For lists and vectors, we can slice the sequence and take any portion we want. How to do similar operations to map objects ?
For example, I have a list of map object,
(def plays [
{:name "Burial", :plays 979, :loved 9}
{:name "Eno", :plays 2333, :loved 15}
{:name "Bill", :plays 979, :loved 9}
{:name "Magma", :plays 2665, :loved 31}])
For each map, I want to slice off plays column, and add rate column with default value, what is the idiomatic way to do this ?
Upvotes: 6
Views: 863
Reputation: 8848
Depending on your use-case, you might also find select-keys
useful in addition to assoc
and dissoc
:
clojure.core/select-keys ([map keyseq]) Returns a map containing only those entries in map whose key is in keys
(select-keys {:name "Eno" :plays 2333 :loved 15} [:name :loved])
;; => {:name "Eno" :loved 15}
Upvotes: 15
Reputation: 33657
assoc
and dissoc
are your friends in this case:
(map #(-> % (dissoc :plays)
(assoc :rate 10)) plays)
Upvotes: 18