Search code examples
clojurelispclojurescript

how to remove a given key from a series of maps in a vector in clojure?


In clojure, given a data structure [{:a "foo" :b "bar"} {:a "biz" :b "baz"}] how would I get [{:b "bar"}{:b "baz"}] the most succinctly?


Solution

  • dissoc is a function for dissociating a key from an associative structure like a map. Here's how you'd do it with one map:

    (dissoc my-map :a)
    

    If you have a sequence of maps, you can map a function over them to dissoc the key(s) from each map:

    (map #(dissoc % :a) the-maps)
    

    This phrasing passes an anonymous function to map, but depending on usage you may want to extract a named function:

    (defn fix-the-map [m]
      (dissoc m :a))
    
    (map fix-the-map the-maps)