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->
Two approaches that do not require writing custom conversion functions - they both employ standard JavaScript functions to loose the custom prototype and thus enable clj->js
to work correctly.
This approach just serializes to JSON and immediately parses it:
(js->clj (-> e js/JSON.stringify js/JSON.parse))
Advantages:
Disadvantages:
This approach is based on Object.assign() and it works by copying all the properties from e
onto a fresh, plain (no custom prototype) #js {}
.
(js->clj (js/Object.assign #js {} e))
Advantages:
Disadvantages:
e
, it won't be converted by clj->js
.Object.assign()
is not supported by old browsers, most notably - IE.