Skip to main content
The 2024 Developer Survey results are live! See the results
Improved the code and the wording and added a better example.
Source Link
MSOACC
  • 3.6k
  • 2
  • 32
  • 51

I do it like

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

// then created aThe following method will convert a Map to a JSON string:

public static getJSONObj():any{
 string {
  const json =return JSON.stringify(Object.fromEntries(map));
}

Example:

const x = new returnMap();
x.set("SomeBool", json;true);
x.set("number1", 1);
x.set("anObj", { name: "joe", age: 22, isAlive: true });

const json = getJSONObj(x);

// Output:
// '{"SomeBool":true,"number1":1,"anObj":{"name":"joe","age":222,"isAlive":true}}'

// and use this method

I do it like

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

// then created a method

public static getJSONObj():any{
    const json = JSON.stringify(Object.fromEntries(map));
    return json;
}

// and use this method

The following method will convert a Map to a JSON string:

public static getJSONObj(): string {
    return JSON.stringify(Object.fromEntries(map));
}

Example:

const x = new Map();
x.set("SomeBool", true);
x.set("number1", 1);
x.set("anObj", { name: "joe", age: 22, isAlive: true });

const json = getJSONObj(x);

// Output:
// '{"SomeBool":true,"number1":1,"anObj":{"name":"joe","age":222,"isAlive":true}}'
Source Link

I do it like

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

// then created a method

public static getJSONObj():any{
    const json = JSON.stringify(Object.fromEntries(map));
    return json;
}

// and use this method