aset does not chain in clojurescript
-
12-06-2021 - |
Question
I found a result in clojurescript whon aset is chained:
(def data (js-obj))
(-> data
(aset "a" "a")
(aset "b" "b"))
(aget data "a") ;=> "a"
(aget data "b") ;=> 'returns nothing'
whereas
(-> 1 inc dec) ;=> returns 1, which is fine
Solution
Ankur is right about why this happens; to "work around" it, you should use the more semantically-descriptive:
(doto data
(aset "a" "a")
(aset "b" "b"))
OTHER TIPS
aset
returns the inserted element and not the array object itself, hence in the case of "b" the data
is not threaded to it from previous "a" call.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow