This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2015-11-24
Channels
- # admin-announcements (25)
- # beginners (132)
- # boot (89)
- # cider (26)
- # clara (12)
- # cljs-dev (10)
- # cljsrn (11)
- # clojure (151)
- # clojure-germany (8)
- # clojure-russia (1)
- # clojurescript (137)
- # cursive (33)
- # datavis (28)
- # datomic (3)
- # devcards (8)
- # hoplon (5)
- # immutant (11)
- # jobs (4)
- # ldnclj (58)
- # lein-figwheel (7)
- # off-topic (95)
- # om (114)
- # onyx (91)
- # parinfer (38)
- # portland-or (1)
- # re-frame (26)
- # reagent (1)
I'm hoping to find a cljs equivalent of Underscore's "indexBy" http://underscorejs.org/#indexBy . Would anyone be able to point me in the right direction?
given a list of json objects, if I group by a key (say: id), the value will be a list correct?
ideally I would have (group-by :id items) => {:1 {:title "title here" :description "description here"} :2 {:title "another title" ....}
Maybe something like this then?
(into {} (map (juxt :age identity) [{:name "moe" :age 40} {:name "larry" :age 50} {:name "curly" :age 60}]))
juxt
is a too clever way of saying "make me a vector of results of applying those functions to something"
juxt ftw
(map (juxt inc dec) [1 2 3]) ;; ([2 0] [3 1] [4 2])
source: https://github.com/clojure/clojure/blob/clojure-1.7.0/src/clj/clojure/core.clj#L2445
That said, depending on how you use it later, you could just use group-by
and just destructure on that, for example:
(for [[id [val & _]] (group-by :age [{:name "moe" :age 40} {:name "larry" :age 50} {:name "curly" :age 60}])]
(println "id=" id "; val=" val))
looks like clojuredocs beat us to it http://clojuredocs.org/clojure.core/juxt#example-542692cfc026201cdc326e12