Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Post Timeline

Commonmark migration
Source Link

The accepted answer wasn't working for me with the javascript object window.performance.timing. This is because Object.keys() doesn't actually return the props for the PerformanceTiming object.

(.keys js/Object (.-timing (.-performance js/window))
; => #js[]

This is despite the fact that the props of PerformanceTiming are indeed iterable with a vanilla JavaScript loop:

for (a in window.performance.timing) {
 console.log(a);
}
// navigationStart
// unloadEventStart
// unloadEventEnd
// ...

The following is what I came up with to convert an arbitrary JavaScript object to a ClojureScript map. Note the use of two simple Google Closure functions.

  • goog.typeOf wraps typeof, which isn't normally accessible to us in ClojureScript. I use this to filter out props which are functions.
  • goog.object.getKeys wraps for (prop in obj) {...}, building up an array result which we can reduce into a map.

#Solution (flat)

Solution (flat)

(defn obj->clj
 [obj]
 (-> (fn [result key]
 (let [v (goog.object/get obj key)]
 (if (= "function" (goog/typeOf v))
 result
 (assoc result key v))))
 (reduce {} (.getKeys goog/object obj))))

Solution (recursive)

Update: This solution will work for nested maps.

(defn obj->clj
 [obj]
 (if (goog.isObject obj)
 (-> (fn [result key]
 (let [v (goog.object/get obj key)]
 (if (= "function" (goog/typeOf v))
 result
 (assoc result key (obj->clj v)))))
 (reduce {} (.getKeys goog/object obj)))
 obj))

The accepted answer wasn't working for me with the javascript object window.performance.timing. This is because Object.keys() doesn't actually return the props for the PerformanceTiming object.

(.keys js/Object (.-timing (.-performance js/window))
; => #js[]

This is despite the fact that the props of PerformanceTiming are indeed iterable with a vanilla JavaScript loop:

for (a in window.performance.timing) {
 console.log(a);
}
// navigationStart
// unloadEventStart
// unloadEventEnd
// ...

The following is what I came up with to convert an arbitrary JavaScript object to a ClojureScript map. Note the use of two simple Google Closure functions.

  • goog.typeOf wraps typeof, which isn't normally accessible to us in ClojureScript. I use this to filter out props which are functions.
  • goog.object.getKeys wraps for (prop in obj) {...}, building up an array result which we can reduce into a map.

#Solution (flat)

(defn obj->clj
 [obj]
 (-> (fn [result key]
 (let [v (goog.object/get obj key)]
 (if (= "function" (goog/typeOf v))
 result
 (assoc result key v))))
 (reduce {} (.getKeys goog/object obj))))

Solution (recursive)

Update: This solution will work for nested maps.

(defn obj->clj
 [obj]
 (if (goog.isObject obj)
 (-> (fn [result key]
 (let [v (goog.object/get obj key)]
 (if (= "function" (goog/typeOf v))
 result
 (assoc result key (obj->clj v)))))
 (reduce {} (.getKeys goog/object obj)))
 obj))

The accepted answer wasn't working for me with the javascript object window.performance.timing. This is because Object.keys() doesn't actually return the props for the PerformanceTiming object.

(.keys js/Object (.-timing (.-performance js/window))
; => #js[]

This is despite the fact that the props of PerformanceTiming are indeed iterable with a vanilla JavaScript loop:

for (a in window.performance.timing) {
 console.log(a);
}
// navigationStart
// unloadEventStart
// unloadEventEnd
// ...

The following is what I came up with to convert an arbitrary JavaScript object to a ClojureScript map. Note the use of two simple Google Closure functions.

  • goog.typeOf wraps typeof, which isn't normally accessible to us in ClojureScript. I use this to filter out props which are functions.
  • goog.object.getKeys wraps for (prop in obj) {...}, building up an array result which we can reduce into a map.

Solution (flat)

(defn obj->clj
 [obj]
 (-> (fn [result key]
 (let [v (goog.object/get obj key)]
 (if (= "function" (goog/typeOf v))
 result
 (assoc result key v))))
 (reduce {} (.getKeys goog/object obj))))

Solution (recursive)

Update: This solution will work for nested maps.

(defn obj->clj
 [obj]
 (if (goog.isObject obj)
 (-> (fn [result key]
 (let [v (goog.object/get obj key)]
 (if (= "function" (goog/typeOf v))
 result
 (assoc result key (obj->clj v)))))
 (reduce {} (.getKeys goog/object obj)))
 obj))
added 355 characters in body
Source Link
Aaron Blenkush
  • 3.1k
  • 2
  • 30
  • 55
<!-- language: lang-clj -->
(defn obj->clj
 [obj]
 (-> (fn [result key]
 (let [v (goog.object/get obj key)]
 (if (= "function" (goog/typeOf v))
 result
 (assoc result key v))))
 (reduce {} (.getKeys goog/object obj))))
(defn obj->clj
 [obj]
 (-> (fn [result key]
 (let [v (goog.object/get obj key)]
 (if (= "function" (goog/typeOf v))
 result
 (assoc result key v))))
 (reduce {} (.getKeys goog/object obj))))
(defn obj->clj
 [obj]
 (-> (fn [result key]
 (let [v (goog.object/get obj key)]
 (if (= "function" (goog/typeOf v))
 result
 (assoc result key (if (goog.isObject v) (obj->clj v) v)))))
 (reduce {} (.getKeys goog/object obj))))
(defn obj->clj
 [obj]
 (if (goog.isObject obj)
 (-> (fn [result key]
 (let [v (goog.object/get obj key)]
 (if (= "function" (goog/typeOf v))
 result
 (assoc result key (obj->clj v)))))
 (reduce {} (.getKeys goog/object obj)))
 obj))
<!-- language: lang-clj -->
(defn obj->clj
 [obj]
 (-> (fn [result key]
 (let [v (goog.object/get obj key)]
 (if (= "function" (goog/typeOf v))
 result
 (assoc result key v))))
 (reduce {} (.getKeys goog/object obj))))
(defn obj->clj
 [obj]
 (-> (fn [result key]
 (let [v (goog.object/get obj key)]
 (if (= "function" (goog/typeOf v))
 result
 (assoc result key (if (goog.isObject v) (obj->clj v) v)))))
 (reduce {} (.getKeys goog/object obj))))
(defn obj->clj
 [obj]
 (-> (fn [result key]
 (let [v (goog.object/get obj key)]
 (if (= "function" (goog/typeOf v))
 result
 (assoc result key v))))
 (reduce {} (.getKeys goog/object obj))))
(defn obj->clj
 [obj]
 (if (goog.isObject obj)
 (-> (fn [result key]
 (let [v (goog.object/get obj key)]
 (if (= "function" (goog/typeOf v))
 result
 (assoc result key (obj->clj v)))))
 (reduce {} (.getKeys goog/object obj)))
 obj))
added 355 characters in body
Source Link
Aaron Blenkush
  • 3.1k
  • 2
  • 30
  • 55

The accepted answer wasn't working for me with the javascript object window.performance.timing. This is because Object.keys() doesn't actually return the props for the PerformanceTiming object.

(.keys js/Object (.-timing (.-performance js/window))
; => #js[]

This is despite the fact that the props of PerformanceTiming are indeed iterable with a vanilla JavaScript loop:

for (a in window.performance.timing) {
 console.log(a);
}
// navigationStart
// unloadEventStart
// unloadEventEnd
// ...

The following is what I came up with to convert an arbitrary JavaScript object to a ClojureScript map. Note the use of two simple Google Closure functions.

  • goog.typeOf wraps typeof, which isn't normally accessible to us in ClojureScript. I use this to filter out props which are functions.
  • goog.object.getKeys wraps for (prop in obj) {...}, building up an array result which we can reduce into a map.

#Solution (flat)

<!-- language: lang-clj -->
(defn obj->clj
 [obj]
 (-> (fn [result key]
 (let [v (goog.object/get obj key)]
 (if (= "function" (goog/typeOf v))
 result
 (assoc result key v))))
 (reduce {} (.getKeys goog/object obj))))

Solution (recursive)

Update: This solution will work for nested maps.

(defn obj->clj
 [obj]
 (-> (fn [result key]
 (let [v (goog.object/get obj key)]
 (if (= "function" (goog/typeOf v))
 result
 (assoc result key (if (goog.isObject v) (obj->clj v) v)))))
 (reduce {} (.getKeys goog/object obj))))

The accepted answer wasn't working for me with the javascript object window.performance.timing. This is because Object.keys() doesn't actually return the props for the PerformanceTiming object.

(.keys js/Object (.-timing (.-performance js/window))
; => #js[]

This is despite the fact that the props of PerformanceTiming are indeed iterable with a vanilla JavaScript loop:

for (a in window.performance.timing) {
 console.log(a);
}
// navigationStart
// unloadEventStart
// unloadEventEnd
// ...

The following is what I came up with to convert an arbitrary JavaScript object to a ClojureScript map. Note the use of two simple Google Closure functions.

  • goog.typeOf wraps typeof, which isn't normally accessible to us in ClojureScript. I use this to filter out props which are functions.
  • goog.object.getKeys wraps for (prop in obj) {...}, building up an array result which we can reduce into a map.

#Solution

(defn obj->clj
 [obj]
 (-> (fn [result key]
 (let [v (goog.object/get obj key)]
 (if (= "function" (goog/typeOf v))
 result
 (assoc result key v))))
 (reduce {} (.getKeys goog/object obj))))

The accepted answer wasn't working for me with the javascript object window.performance.timing. This is because Object.keys() doesn't actually return the props for the PerformanceTiming object.

(.keys js/Object (.-timing (.-performance js/window))
; => #js[]

This is despite the fact that the props of PerformanceTiming are indeed iterable with a vanilla JavaScript loop:

for (a in window.performance.timing) {
 console.log(a);
}
// navigationStart
// unloadEventStart
// unloadEventEnd
// ...

The following is what I came up with to convert an arbitrary JavaScript object to a ClojureScript map. Note the use of two simple Google Closure functions.

  • goog.typeOf wraps typeof, which isn't normally accessible to us in ClojureScript. I use this to filter out props which are functions.
  • goog.object.getKeys wraps for (prop in obj) {...}, building up an array result which we can reduce into a map.

#Solution (flat)

<!-- language: lang-clj -->
(defn obj->clj
 [obj]
 (-> (fn [result key]
 (let [v (goog.object/get obj key)]
 (if (= "function" (goog/typeOf v))
 result
 (assoc result key v))))
 (reduce {} (.getKeys goog/object obj))))

Solution (recursive)

Update: This solution will work for nested maps.

(defn obj->clj
 [obj]
 (-> (fn [result key]
 (let [v (goog.object/get obj key)]
 (if (= "function" (goog/typeOf v))
 result
 (assoc result key (if (goog.isObject v) (obj->clj v) v)))))
 (reduce {} (.getKeys goog/object obj))))
added 9 characters in body
Source Link
Aaron Blenkush
  • 3.1k
  • 2
  • 30
  • 55
Loading
actually use the function I referenced
Source Link
Aaron Blenkush
  • 3.1k
  • 2
  • 30
  • 55
Loading
Source Link
Aaron Blenkush
  • 3.1k
  • 2
  • 30
  • 55
Loading
lang-clj

AltStyle によって変換されたページ (->オリジナル) /