4

I have such a environment to start from:

(defn field-name "blah")

(defn obj (js* "{
                 list: [1,2,3,4,5],
                 blah: \"vtha\",
                 o: { answer: 42 }
               }")

How do I get (idiomatic way) blah field using field name var?

(aget obj field-name)

works, but it is intended for arrays (docs say)

1

1 Answer 1

6

You can use goog.object/get and I think this is idiomatic way to access the properties.

I would also recommend binaryage/cljs-oops that is addressing this very problem.

Sign up to request clarification or add additional context in comments.

1 Comment

And if you need to go deeper, goog.object/getValueByKeys is nice: (goog.object/getValueByKeys obj #js ["o" "answer"]) yields 42 and (goog.object/getValueByKeys obj #js ["list" 2]) yields 3. Also see blog.fikesfarm.com/posts/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.