Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala Play - How to convert a list of Scala Strings into an Array of javascript Strings (avoiding the " issue)?

When converting a Scala list of Strings into a javascript Array of Strings with the Play template engine, you probably start with something like this ...

var strArray = [@scalaListOfStrings.mkString(",")];

... and will find out that this is not working, because the quotes around the strings are missing. Next you might try something like this ...

var strArray = [@scalaListOfStrings.map(s => "\"" + s + "\"").mkString(",")];

... only to find out that this will wrap the strings in " and not ". The only way I was able to make this work was with ...

var strArray = [@Html(scalaListOfStrings.map(s => "\"" + s + "\"").mkString(","))];

... and my question is: Is this the best/only way to do this?

like image 622
Roland Tritsch Avatar asked May 12 '13 06:05

Roland Tritsch


1 Answers

You can rely on the Json.toJson() method to make the conversion

@import play.api.libs.json._

var strArray = @Json.stringify(Json.toJson(List("hello", "world", "everybody")))
like image 101
Julien Lafont Avatar answered Oct 21 '22 04:10

Julien Lafont