[JS-INTEROP] "for in" and "for of" loop of dynamic objects

Unfortunately I don’t think it is possible to achieve the for (key in obj) syntax (*). The reason is that Kotlin doesn’t allow defining extension functions with dynamic receiver.

I think your best bet would be to create a helper function:

inline fun keys(json: dynamic) = js("Object").keys(json).unsafeCast<Array<String>>()

and use it like so: for (i in keys(obj))

(*) It is possible in kotlin stdlib with some black magic. That’s why for (i in obj) compiles. Unfortunately it only supports iterating over arrays, objects with iterator function, and Kotlin Iterable descendants.

2 Likes