1

If I have a vector of maps

(def v [{:key1 "value 1" :key2 "value2"} {:key1 "value 3" :key2 "value4"}])

and a map

(def m {:key3 "value2" :key4 "value5"})

How to add map m to all the maps in vector v where the values of 2 given keys (in this case key2 and key3) are equal?

The expected result would be this:

[{:key1 "value 1" :key2 "value2" :key3 "value2" :key4 "value5"} {:key1 "value 3" :key2 "value4"}]

1 Answer 1

3

You want to merge each map with m3 if key2 and k3 are equal:

(map (fn [x] (if (= (:key2 x) (:key3 m)) (merge m x) x)) v)

=>

({:key3 "value2", :key2 "value2", :key4 "value5", :key1 "value 1"} {:key2 "value4", :key1 "value 3"})
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, mapv if the result needs to be a vector.
I have one more question, I have a situation like this,vector is (defn v2 []( map #(zipmap (create-keys ["url" "title"]) %) (func-contents ["url" "title"] "//event" data-url))) data-url is in this question bit.ly/1x83gYH and map is (defn v1[] (map #(zipmap (create-keys ["name" "url"]) %) (func-contents ["name" "url"] "//artist" "ws.audioscrobbler.com/2.0/…))). In this case it doesn't work, it adds the map anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.