javascript anonymous object in kotlin

前端 未结 6 2035
终归单人心
终归单人心 2021-02-07 09:18

how to create JavaScript anonymous object in kotlin? i want to create exactly this object to be passed to nodejs app

var header = {“content-type”:”text/plain” ,          


        
6条回答
  •  没有蜡笔的小新
    2021-02-07 09:34

    Here is another solution:
    Define the following helper function

    fun jsObject(vararg pairs: Pair): dynamic {
        val result = js("({})")
        for ((key, value) in pairs) {
            result[key] = value
        }
        return result
    }
    

    You can then use it as follows

    val header = jsObject("content-type" to "text/plain", "content-length" to 50)
    

提交回复
热议问题