Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ScalaJS: How to convert String to String?

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?

like image 910
Ilya Lakhin Avatar asked Apr 12 '14 12:04

Ilya Lakhin


2 Answers

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.

like image 63
sjrd Avatar answered Oct 09 '22 01:10

sjrd


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
like image 29
gzm0 Avatar answered Oct 09 '22 01:10

gzm0