Fork me on GitHub
#clojurescript
<
2023-03-23
>
Chris McCormick08:03:09

Sorry I should have clarified that I mean on nested JS objects.

Chris McCormick08:03:02

(some-> (clj->js {:a {:b {:c 1}}}) (aget "a" "d"))

Chris McCormick08:03:27

Wait am I doing something dumb here. :thinking_face:

Chris McCormick08:03:59

Yep so this throws:

(some-> (clj->js {:a {:b {:c 1}}}) (aget "a" "d" "n"))

Chris McCormick08:03:00

Ok got it:

(some-> (clj->js {:a {:b {:c 1}}}) (aget "a") (aget "d") (aget "c"))

👍 2
thheller08:03:36

note that aget is for array, using it with strings is frowned upon

thheller08:03:40

use unchecked-get instead

👀 2
thheller08:03:16

or gobj/get with (:require [goog.object :as gobj])

borkdude09:03:49

looks at docstring of unchecked-get:

Macro
  INTERNAL.
😅

borkdude09:03:33

(I've been using it as well, for some extra perf)

borkdude09:03:45

This might also be useful:

cljs.user=> (goog.object/getValueByKeys (clj->js {:a {:b {:c 2}}}) "a" "b" "c")
2
cljs.user=> (goog.object/getValueByKeys (clj->js {:a {:x {:c 2}}}) "a" "b" "c")
nil

🙏 2
shaunlebron20:04:51

@UUSQUGUF3 I saw this library used at my job. I like it: https://github.com/applied-science/js-interop > To cohere with Clojure semantics, j/get and j/get-in return nil if reading from a nil object instead of throwing an error.

js:   adventurer.dog?.name
cljs: (j/get-in adventurer [:dog :name])

❤️ 2