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?
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?
1. for is not a loop
2. the canonical idiom is (for [[idx choice] (map-indexed vector choices)] ...)
nice one thanks
Well, it is better if you can get a unique id :) for the react key
right - but the key should be tied to the identity of the item, not the order