redhands
redhands

Reputation: 357

Clojure merge key values in hash map

So I have the below hash map of k/v:

(def data [{:ID "0000010890" :CREDIT "EM 1"}

{:ID "0000010890" :CREDIT "FLOW 1"}

{:ID "0000010890" :CREDIT "EM 1"}

{:ID "0000010890" :CREDIT "FLOW 1"}

{:ID "0000010809" :CREDIT "FLOW 1"}])

which I need to merge in to this:

[{:ID "0000010890" :CREDIT "EM 1, FLOW 1"}

{:ID "0000010809" :CREDIT "FLOW 1"}]

Can anyone advise the best way to go about this in clojure?

My attempt:

(defn- create-issue-summary-2 [data]
            (->> data
                 (group-by :ID)
                 (map (fn [[k v]] (into {:ID k} (apply merge-with (map #(dissoc % :ID) v)))))))

Which results in

({:ID "0000010890", :CREDIT "FLOW 1"} 

{:ID "0000010809"})

Upvotes: 1

Views: 1009

Answers (1)

Ankur
Ankur

Reputation: 33637

(map (fn [[k v]]
       (into {} [[:ID k]
                 [:CREDIT (apply str (interpose ", " (distinct (map :CREDIT v))))]]))
     (group-by :ID data))

Upvotes: 1

Related Questions