Search code examples
clojure

Map from list of maps


My problem is next, i have list of maps, for example:

({:id 1 :request-count 10 ..<another key-value pair>..} 
 {:id 2 :request-count 15 ..<another key-value pair>..} 
 ...)

Need create map with records in which, key is value of 'id' and value is value of 'request-count', for each map from prev example, like:

{1 10
 2 15
 ...}

I know how to do this. My question is - standard library have function for achieve this? Or maybe i can achiev this with combination few function, without 'reduce'?


Solution

  • Use the juxt function to generate a sequence of pairs, and then toss them into a map:

    (into {} (map (juxt :id :request-count) data))
    

    Example:

    user=> (def data [{:id 1 :request-count 10 :abc 1}
      #_=>            {:id 2 :request-count 15 :def 2}
      #_=>            {:id 3 :request-count 20 :ghi 3}])
    #'user/data
    user=> (into {} (map (juxt :id :request-count) data))
    {1 10, 2 15, 3 20}
    

    Be aware that if there is more than one map in data with the same :id, then the last one encountered by map will be the one that survives in the output map.