3

In index-dev.html I load a local JavaScript file:

<script type="text/javascript" src="map.js"></script>

map.js structurally (not literally) looks like this:

var a2b = {
  "a": "My life closed twice before its close—",
  "b": "It yet remains to see",
  "c": "If Immortality unveil",
  "d": "A third event to me",

  "e": "So huge, so hopeless to conceive",
  "f": "As these that twice befell.",
  "g": "Parting is all we know of heaven,",
  "h": "And all we need of hell.",

  "z": "Emily Dickinson, 1830-1886"
}

I figured out how to load this object into Scala.js:

val a2b = js.Dynamic.global.a2b

Now a2b is of type Dynamic. What I want is Map[String,String].

I tried this:

val a2b = js.Dynamic.global.a2b.asInstanceOf[Map[String,String]]

but that didn't work. What should I do to get a Map[String,String]?

gknauth
  • 2,310
  • 2
  • 29
  • 44
  • This looks similar: http://stackoverflow.com/questions/31655070/how-to-convert-map-to-json-object-in-scala-js – gknauth Oct 28 '16 at 17:36

1 Answers1

7

The key thing is that it isn't a Map, which is why it's not working. But it is a js.Dictionary. So use that for your asInstanceOf instead, and if you actually need a Scala Map, use the toMap function on that:

val a2b = js.Dynamic.global.a2b.asInstanceOf[js.Dictionary[String]].toMap
Justin du Coeur
  • 2,699
  • 1
  • 14
  • 19