Hi! I'm trying to construct a Js object in Kotlin....
# javascript
c
Hi! I'm trying to construct a Js object in Kotlin. As I understand, I can do the following when I have a list of variables, and I want to put them into a Js object.
Copy code
val myObject: dynamic = js("{}")

params.forEach { param ->
  myObject[param.key] = param.value
}
But can I somehow convert this class
Copy code
class MyClass(
    val variable1: String,
    val variable2: String,
    val variable3: String
)
into a Js object, like this?
Copy code
{
  variable1: "",
  variable2: "",
  variable3: ""
}
If i try
myObject[key] = Json.encodeToString(MyClass(" ", " ", " "))
it will be a simple string instead of a js object.
t
сс @Sergei Grishchenko
a
You can use
asDynamic()
extension :
Copy code
val myVariable = MyClass("a", "b", "c")
val myObject = myVariable.asDynamic()
// then you can use your object however you want

myObject["something"] = "foo"
t
We already have declarations for Object.entries Less dynamic = less problems