Reputation: 1063
I have a vector structure like this...
[{:email "[email protected]", :password "pass"}
{:email "[email protected]", :password "pass2"}
{:email "[email protected]", :password "pass3"}
{:email "[email protected]", :password "pass4"}]
How can i make a map out of this that will look like this?The two email values must be same...
{{"[email protected]"{:email "[email protected]", :password "pass"}},
{"[email protected]"{:email "[email protected]", :password "pass2"}},
{"[email protected]" {:email "[email protected]", :password "pass3"}} ,
{"[email protected]"{:email "[email protected]", :password "pass4"}}
I am a bit new to clojure,so any help is appreciated.
Upvotes: 0
Views: 267
Reputation: 10395
Another alternative (though your output will be nested in a vector) is to use group-by
:
user=> (def a [{:email "[email protected]", :password "pass"}
{:email "[email protected]", :password "pass2"}
{:email "[email protected]", :password "pass3"}
{:email "[email protected]", :password "pass4"}])
#'user/a
user=> (group-by :email a)
{"[email protected]" [{:email "[email protected]", :password "pass"}], "[email protected]" [{:email "[email protected]", :password "pass2"}], "[email protected]" [{:email "[email protected]", :password "pass3"}], "[email protected]" [{:email "[email protected]", :password "pass4"}]}
user=>
Upvotes: 0
Reputation: 876
(def a [{:email "[email protected]", :password "pass"}
{:email "[email protected]", :password "pass2"}
{:email "[email protected]", :password "pass3"}
{:email "[email protected]", :password "pass4"}])
(zipmap (map :email a) a)
Creates a sequence of the email addresses and then zips that and the vector of the original maps into a new map with the addresses as the keys and the original maps as the values.
(think of the list of emails presented vertically on the left and the original map entries presented vertically on the right and a zip moving down joining them together)
Upvotes: 3