I'm learning clojure, and I want to take a vector of names, in last name -> first name order, of multiple people, and convert it to a vector of maps...
["Pan" "Peter" "Mouse" "Mickey"]
Should become...
[{:firstName Peter, :lastName Pan} {:firstName Mickey, :lastName Mouse}]
I've tried this, which doesn't work...
(for [[lastName firstName]
(list ["Pan" "Peter" "Mouse" "Mickey"])]
{:firstName firstName, :lastName lastName}
)
If I remove the list it turns the first/last name into individual characters.
I'm at a complete loss as to how to go about doing this.
You can convert your input vector into a sequence of pairs with partition:
(def names ["Pan" "Peter" "Mouse" "Mickey"])
(partition 2 names)
You can convert a pair of lastName/firstName into a map using zipmap e.g.
(zipmap [:lastName :firstName] ["Pan" "Peter"])
You can convert the sequences of pairs into a sequence of maps using map:
(map #(zipmap [:lastName :firstName] %) (partition 2 names))
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