从 json 对象初始化 Typescript 中的 Map<string,string>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45212331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:44:34  来源:igfitidea点击:

initializing a Map<string,string> in Typescript from json object

javascripttypescript

提问by Laurent T

I have a class in Typescript containing a map:

我在 Typescript 中有一个包含地图的类:

  public map:Map<string,string>;

  constructor() {
    let jsonString = {
      "peureo" : "dsdlsdksd"
    };
    this.map = jsonString;
   }

Problem is that the initialization doesn't work? jsonString is something that I will receive from a Java Object serialized to Json...

问题是初始化不起作用?jsonString 是我将从序列化为 Json 的 Java 对象接收的东西...

Anyone can help?

任何人都可以帮忙吗?

Thanks

谢谢

回答by wa4_tasty_elephant

You must iterate over the members of the object and add it to the map:

您必须遍历对象的成员并将其添加到地图中:

public map: Map<string, string> = new Map<string, string>();

constructor() {
    let jsonString = {
        "peureo": "dsdlsdksd"
    };

    for (let member in jsonString) {
        this.map.set(member, jsonString[member]);
    }
}

回答by Laurent T

I originally flagged this question as a possible duplicate, but having thought about it a bit more I think there's enough differences between the plain Javascript and TypeScript that it is a standalone question ... my bad!

我最初将这个问题标记为可能的重复问题,但经过深思熟虑后,我认为普通 Javascript 和 TypeScript 之间存在足够的差异,以至于它是一个独立的问题……我的错!

When the JSON is deserialized, it results in a plain object.

当 JSON 被反序列化时,它会生成一个普通对象。

Mapis a specific class, and you can't just cast a plain object in to a Mapobject.

Map是一个特定的类,您不能只是将普通对象转换为Map对象。

The documentation for Mapsays that it's constructor takes:

的文档Map说它的构造函数需要:

An Array or other iterable object whose elements are key-value pairs. Each key-value pair is added to the new Map; null values are treated as undefined.

一个 Array 或其他可迭代对象,其元素是键值对。每个键值对都被添加到新的 Map 中;空值被视为未定义。

Unfortunately plain objects are not iterable, so we can't just pass the object to Map'sconstructor.

不幸的是,普通对象是不可迭代的,所以我们不能只将对象传递给Map's构造函数。

What we can do is loop over the properties, check if they're strings, and then add them to the map.

我们可以做的是遍历属性,检查它们是否是字符串,然后将它们添加到地图中。

let objectStringPropertiesToMap = (obj: Object) => {
    let map = new Map();

    for (let key in obj) {
        let val = obj[key];

        if (typeof val == "string")
            map.set(key, val);
    }
};

Which would give you code something like this:

这会给你这样的代码:

let obj = {
    "peureo" : "dsdlsdksd"
};

let objectStringPropertiesToMap = (obj: Object) => {
    let map = new Map();

    for (let key in obj) {
        let val = obj[key];

        if (typeof val == "string")
            map.set(key, val);
    }

    return map;
};

let map = objectStringPropertiesToMap(obj);

document.getElementById("output").innerHTML = map.get("peureo");

Which can be seen working on jsFiddle.

可以看到在jsFiddle 上工作。