Skip to content

Instantly share code, notes, and snippets.

@lynaghk
Created August 11, 2011 23:21
Show Gist options
  • Save lynaghk/1141054 to your computer and use it in GitHub Desktop.
Save lynaghk/1141054 to your computer and use it in GitHub Desktop.
Clojure sequentials & maps into JavaScript arrays and objects
(defn jsArr
"Recursively converts a sequential object into a JavaScript array"
[seq]
(.array (vec (map #(if (sequential? %) (jsArr %) %)
seq))))
(defn jsObj
"Convert a clojure map into a JavaScript object"
[obj]
(.strobj (into {} (map (fn [[k v]]
(let [k (if (keyword? k) (name k) k)
v (if (keyword? v) (name v) v)]
(if (map? v)
[k (jsObj v)]
[k v])))
obj))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment