I have a vector of errors that looks like this:
(def messages
[{:error "Name is required"}
{:error "Last name is required"}
{:error "Date of birth is in the wrong format"}])
(->> messages
(map :error)
(remove string/blank?)
(string/join ", ")
(#(string/replace % #", ([^,]+)$" " and $1")))
The above form will output the following result:
"Name is required, Last name is required and Date of birth is in the wrong format"
It uses a threaded macro to map through all :error
keywords, removing blanks (and nils), joining by comma and then replacing the last comma with an and to read nicely.
This gives me a comma-separated list
Top comments (0)