function/macro | since v0.0-927 | clojure.core/aset | Edit |
(aset array idx val)
(aset array idx idx2 & idxv)
Sets val
at index i
in a JavaScript array.
(def a #js [1 2 3])
(aset a 0 "foo")
a
;;=> #js ["foo" 2 3]
Set nested elements with the additional idxs
arguments.
(def a #js [1 2 #js [3 4]])
(aset a 2 0 "foo")
a
;;=> #js [1 2 #js ["foo" 4]]
For JavaScript objects, use goog.object/set
.
(require 'goog.object)
(def obj #js {:foo 1})
(goog.object/set obj "foo" "bar")
obj
;;=> #js {:foo "bar"}
Sets the value at the index/indices. Works on JavaScript arrays. Returns val.
(defn aset
([array idx val]
(cljs.core/aset array idx val))
([array idx idx2 & idxv]
(apply aset (aget array idx) idx2 idxv)))
(core/defmacro aset
([array idx val]
(core/case (ana/checked-arrays)
:warn `(checked-aset ~array ~idx ~val)
:error `(checked-aset' ~array ~idx ~val)
(core/list 'js* "(~{}[~{}] = ~{})" array idx val)))
([array idx idx2 & idxv]
(core/case (ana/checked-arrays)
:warn `(checked-aset ~array ~idx ~idx2 ~@idxv)
:error `(checked-aset' ~array ~idx ~idx2 ~@idxv)
(core/let [n (core/dec (count idxv))
astr (apply core/str (repeat n "[~{}]"))]
`(~'js* ~(core/str "(~{}[~{}][~{}]" astr " = ~{})") ~array ~idx ~idx2 ~@idxv)))))