【问题标题】:How do I turn a JavaScript object into Map[String,String]?如何将 JavaScript 对象转换为 Map[String,String]?
【发布时间】:2016-10-29 01:41:47
【问题描述】:

在index-dev.html我加载了一个本地的JavaScript文件:

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

map.js 在结构上(不是字面意思)看起来像这样:

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"
}

我想出了如何将这个对象加载到 Scala.js 中:

val a2b = js.Dynamic.global.a2b

现在a2b 的类型为Dynamic。我要的是Map[String,String]。

我试过这个:

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

但这没有用。我应该怎么做才能获得Map[String,String]?

【问题讨论】:

标签: scala.js


【解决方案1】:

关键是它不是Map,这就是它不起作用的原因。但它是js.Dictionary。因此,请将其用于您的 asInstanceOf,如果您确实需要 Scala Map,请使用 toMap 函数:

val a2b = js.Dynamic.global.a2b.asInstanceOf[js.Dictionary[String]].toMap

【讨论】:

    猜你喜欢
    • 2021-11-17
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    • 1970-01-01
    • 2016-07-29
    • 2013-05-24
    • 2014-01-29
    • 1970-01-01
    相关资源
    最近更新 更多