This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
Quick question (I think). I have a collection of maps. Each map will contain the same 2 keys, but different values. I’d like to merge those maps by one key and group the other key’s values into a vector.
Likely better explained here: https://www.refheap.com/111477
I’m sure there must be some elegant way using merge
or merge-with
or something similar to accomplish this, but I’m hitting a wall.
given this:
({:id 1, :title "Star Wars"} {:id 1, :title "Indiana Jones"} {:id 1, :title "E.T."} {:id 2, :title "Rocky”})
return this:
({:id 1, :title ["Star Wars" "Indiana Jones" "E.T."]} {:id 2, :title ["Rocky"]})
oooh! That definitely gets me much closer!
thanks
i should be able to finish it from there
i knew there was likely something built in and elegant. I was going to write a huge function and map it over the collection
this ended up getting me the result I was looking for
(->> '({:id 1 :title "Star Wars"} {:id 1 :title "Indiana Jones"} {:id 1 :title "E.T."} {:id 2 :title "Rocky"})
(group-by :id)
(map (fn [[k v]] {:id k :titles (mapv :title v)})))