Mau
Mau

Reputation: 73

Filter on the values of multiple keys in a map entry and return a map with those entries Clojure

I am new to clojure and need some help.

In clojurescript I build up a html table using a map (stored in atom) e.g.

[{:id 2, :category "Big bang theory", :name "The Big Bang!"} 
{:id 3, :category "The big Lebowski", :name "Ethan Coen"}
{:id 4, :category "Chitty Chitty Bang Bang", :name "Roald Dahl"}]

I want to create a search that searches for a word (i.e. "ban") en return a map with those entries that have that word (or part of it) in one of its key values.

In case of "ban" it should return

[{:id 2, :category "Big bang theory", :name "The Big Bang!"} 
{:id 4, :category "Chitty Chitty Bang Bang", :name "Roald Dahl"}]

Based on the above map the table updates with only those two entries.

I found some interesting solutions, but they all focus on one key (i.e. :category or :name) but not all keys in the map entry.

I think this tries to achieve the same, but I don't think someone gave the answer. Any help is appreciated :D

Upvotes: 2

Views: 182

Answers (1)

Michiel Borkent
Michiel Borkent

Reputation: 34800

(def maps
  [{:id 2, :category "Big bang theory", :name "The Big Bang!"}
   {:id 3, :category "The big Lebowski", :name "Ethan Coen"}
   {:id 4, :category "Chitty Chitty Bang Bang", :name "Roald Dahl"}])

(filter
 #(some
   (fn [v]
     (when (string? v)
       (-> v
           (str/lower-case)
           (str/includes? "ban"))))
   (vals %))
 maps)

Upvotes: 6

Related Questions