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