ClojureScript - convert arbitrary JavaScript object to Clojure Script map

前端 未结 4 1481
野趣味
野趣味 2021-02-13 20:10

I am trying to convert a Javascript object to a Clojure. However, I get the following error :

 (js/console.log (js->clj e)) ;; has no effect
 (pprint (js->         


        
4条回答
  •  情深已故
    2021-02-13 20:47

    js->clj only works for Object, anything with custom constructor (see type) will be returned as is.

    see: https://github.com/clojure/clojurescript/blob/master/src/main/cljs/cljs/core.cljs#L9319

    I suggest doing this instead:

    (defn jsx->clj
      [x]
      (into {} (for [k (.keys js/Object x)] [k (aget x k)])))
    

    UPDATE for correct solution see Aaron's answer, gotta use goog.object

提交回复
热议问题