6

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?

Ilya Lakhin
  • 1,904
  • 1
  • 16
  • 31

2 Answers2

4

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.

Community
  • 1
  • 1
sjrd
  • 21,805
  • 2
  • 61
  • 91
2

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
gzm0
  • 14,752
  • 1
  • 36
  • 64