gimiarn1801
gimiarn1801

Reputation: 131

How to filter all the elements from a list of hashmaps given another list? Clojure

Consider the following:

list of hashmaps '({:key 1 :other-key "hello"} {:key 2 :other-key "bye"})
some list of keys: '(1)
;; I want the result to be:
[{:key 1 :other-key "hello"}]

Is there a simple way to filter this out?

Upvotes: 0

Views: 137

Answers (2)

Michiel Borkent
Michiel Borkent

Reputation: 34870

You could do it like this:

(def l1 '({:key 1, :other-key "hello"} {:key 2, :other-key "bye"}))
(def l2 '(1))

(filter #(contains? (set l2) (:key %)) l1)

Upvotes: 2

gimiarn1801
gimiarn1801

Reputation: 131

Ended up doing something like this:

(filter #(some #{(:key %)} some-list) list-of-hashmaps)

Upvotes: 1

Related Questions