Iterating over kotlin.js data class


The solution for iterating over data class in Kotlin.jvm side is via reflection, but that is unavailable in javascript. What is the non-explict alternative in javascript?

You can use good old js(Object.keys(...)), but in some cases it provides really strange results due to name mangling in Kotlin-JS. My recommendation is not to use reflections at all. If you need iteration over properties, use statically typed delegates on top of map like this:

inline class MyClass(val map: Map<String, Any>){
  val a: Any by map

1 Like

That does not seem to work with javascript Kotlin, compiler says “missing SetValues…”.

Here is a working example:

class MyClass(val map: MutableMap<String, Any>){
  var a: Any by map

You need MutableMap if you need to mutate it and delegates are not allowed in inline classed (yet at least).

Thanks, i think that provides a solution.