Let's assume I have a list of Strings: List[String]
. And I want to convert it to the interoperable JavaScript array of JavaScript Strings: js.Array[js.String]
. How to do that?
The easiest way of doing that is the following:
myList.map(x => x: js.String).toArray
This can be factored out in an implicit conversion if you need it more than once.
Edit: this answer is obsolete. See @gzm0's answer.
Note that as of Scala.js 0.5.x (current version as of this writing is 0.6.2), there is no difference anymore between java.lang.String
and js.String
. Hence you can do:
import scala.scalajs.js.JSConverters._ // Scala.js >= 0.5.4
val list: List[String] = ???
val jsList: js.Array[String] = list.toJSArray
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With