3

I have a vector of maps with same keys:

(def items [{:id 1 :name "first item"}
            {:id 2 :name "second item"}])

I can uppercase the value of the :name key in the first map in the vector:

(update-in items [0 :name] clojure.string/upper-case)
=> [{:id 1, :name "FIRST ITEM"} {:id 2, :name "second item"}]

How can I uppercase every :name key in every map? I expect this:

[{:id 1, :name "FIRST ITEM"} {:id 2, :name "SECOND ITEM"}]
koszti
  • 131
  • 1
  • 8
  • Similar useful questions http://stackoverflow.com/q/22359975 and http://stackoverflow.com/q/1676891. – glts Apr 03 '16 at 08:19

1 Answers1

3

This should do it:

(map #(update-in % [:name] clojure.string/upper-case) items)

The % sign stands in for each map in items in the function expression.

jmargolisvt
  • 5,722
  • 4
  • 29
  • 46
  • 5
    If you're using Clojure 1.7, you could use [`update`](http://clojuredocs.org/clojure.core/update) instead of `update-in`. – Sam Estep Apr 03 '16 at 02:09