使用动态属性名称将属性分配给JavaScript对象

我想这样做,但我不知道,我不能用js()插入任何动态数据,因为js()只接受常量字符串参数(或有办法做到这一点?)

val doc: dynamic = Any() doc._id = name data.forEach { it.forEach { entry -> // need to set property of the doc using entry.key as the property name with entry.value } } 

您可以使用索引访问 ,就像使用javascript括号访问表示法一样,例如:

 val doc: dynamic = Any() doc._id = name data.forEach { it.forEach { entry -> // v--- kotlin process the brackets []= as a set operator doc[entry.key] = entry.value; } }