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]?

1

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.