How to group a collection of maps by multiple keys?
For example:
(def m1 [{:a 1 :b 2 :c 3}
{:a 1 :b 2 :c 4}
{:a 1 :b 4 :c 3}
{:a 1 :b 4 :c 3}])
(group-by-x [:a :b] m1)
I'd like to return this:
[{:a 1 :b 2} [{:a 1 :b 2 :c 3}{:a 1 :b 2 :c 4}],
{:a 1 :b 4} [{:a 1 :b 4 :c 3}{:a 1 :b 4 :c 3}]]
(group-by #(select-keys % [:a :b]) m1)
This returns a map:
{{:b 2, :a 1} [{:a 1, :c 3, :b 2} {:a 1, :c 4, :b 2}],
{:b 4, :a 1} [{:a 1, :c 3, :b 4} {:a 1, :c 3, :b 4}]}
To get exactly the return value you specified, wrap it in (vec (apply concat ...))
:
(vec (apply concat (group-by #(select-keys % [:a :b]) m1)))
; => as in the question text
This is equivalent, but perhaps prettier:
(->> (group-by #(select-keys % [:a :b]) m1)
(apply concat)
vec)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With