Fork me on GitHub
#reagent
<
2020-07-06
>
Jacob Emcken16:07:09

I find my self keep wanting to have the index available when doing for loops (when they are based of a list or vector:

(def choices ["A" "B" "C"])

[:select {:on-change #(js/console.log (.-value (.-target %))}
 (for [choice choices]
   [:option {:value index} choice])] ; <- how do I get the index?

noisesmith16:07:35

1. for is not a loop 2. the canonical idiom is (for [[idx choice] (map-indexed vector choices)] ...)

❤️ 3
Jacob Emcken16:07:15

nice one thanks

David Pham17:07:14

Well, it is better if you can get a unique id :) for the react key

noisesmith17:07:21

right - but the key should be tied to the identity of the item, not the order

💯 3
Jacob Emcken16:07:13

I've handled it before by changing choices to a map: {0 "A", 1 "B", 2 "C"} or using map for the iteration. But the for loop looks soo much better. Is the some "secret" that I'm overlooking?