Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 368
Laurent T Avatar asked Oct 23 '25 21:10

Laurent T


1 Answers

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]);
    }
}
like image 179
wa4_tasty_elephant Avatar answered Oct 26 '25 10:10

wa4_tasty_elephant