Search code examples
vectormergeclojuremaps

Merge two vectors of maps in clojure based on index


I have two vectors of maps in clojure, and I wish to merge them so that it is a single vector of maps, but the maps at each index are merged. I was just wondering the best way to do this.

For example:

[{:sku "e1" :name "example1"} {:sku "e2" :name "example2"}]
[{:color "Blue" :price 9.99} {:color "Red" :price 15.99}]

Would be merged to:

[{:sku "e1" :name "example1" :color "Blue" :price 9.99} {:sku "e2" :name "example2" :color "Red" :price 15.99}]

Solution

  • Use mapv and merge:

    (mapv merge 
          [{:sku "e1" :name "example1"} {:sku "e2" :name "example2"}]
          [{:color "Blue" :price 9.99} {:color "Red" :price 15.99}])
    
    => [{:sku "e1", :name "example1", :color "Blue", :price 9.99} {:sku "e2", :name "example2", :color "Red", :price 15.99}]