Search code examples
clojure

Filter a complex vector in clojure


I have a mocked data of groups, each group contains users. here is the data I have:

(def group-list 
     [{:name "group1"
       :id "group1"
       :members [{:name "Bartek" :id "mem1"}
                 {:name "Quentin"  :id "mem2"}]}
      {:name "group2"
       :id "group2"
       :members [{:name "Bartek_test" :id "mem3"}
                 {:name "Quentin"  :id "mem2"}]}
      {:name "group3"
       :id "group3"
       :members [{:name "Bartek1" :id "mem1"}]}])

What I want to do is to create a function which will take user id as a parameter, search in group-list and return the full record. For example, if the id will be mem2 method should return {:name "group2" :id "group2" :members [{:name "Bartek_test" :id "mem3"} {:name "Quentin" :id "mem2"}]}

I am a new to clojure so it would be great if anybody gives some idea. Thank you


Solution

  • (defn get-groups-by-member [member-id]
      (filter (fn [x]
                (some #(= member-id %)
                      (map #(get % :id)
                           (x :members))))
              (get-group-list)))