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

28,590

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]);
    }
}
Share:
28,590
Laurent T
Author by

Laurent T

Updated on June 27, 2021

Comments

  • Laurent T
    Laurent T almost 3 years

    I have a class in Typescript containing a map:

    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.

    Anyone can help? Thanks!