Search code examples
clojure

How can I update a map using the `update` function?


   (def p {:name "James" :age 26})

I'm trying update method, like

(update p :name "David") 

which does not work since the second argument has to be a function.


Solution

  • Try this:

    (assoc p :name "David")
    

    Please see this list of documentation, especially the Clojure CheatSheet! See also assoc-in and update-in as described under

    Collections -> Maps
    

    P.S. What you have there is a Clojure map value, which is different than an object in JavaScript or a JSON string.