0

I have this class:

export class Example {

  id: number;
  name: string;
  map: Map<string,boolean>;

  constructor(id: number, name: string, map: Map<string,boolean>) {
    this.id = id;
    this.name = name;
    this.map = map;
  }

}

And when I try to call the get() on the map attribute of an instance like this:

let temp = example.map.get("key");

I get this error:

ERROR TypeError: example.map.get is not a function

Also I'm using Spring boot so how can I make sure I'm sending and receiving the map attribute without messing it up?

What am I doing wrong? Why isn't it recognized as a Map?

And this returns false:

map instanceof Map

And the empty attribute looks like this when I show it on the website: \[object Object\]

Edit: I create instances in 2 ways:

  1. When the data arrives from the backend:

in the service.ts file:

getExamples(): Observable<Example[]>{
   return this.http.get<Example[]>(this.baseUrl);
}

in the component.ts file:

this.exampleService.getExamples().subscribe((data: Example[]) => {
   console.log('Received examples: ',data);
   this.examples = data;
});

I logged it and the Map attribute looks like this in the data:

Object { "text": true }

  1. When the user creates a new instance with a Form:
    const example= this.exampleForm.value as Example;
  • 3
    Please provide the code where you initialize a new Example object. Your class definition looks fine, so you are probably not constructing the Map parameter correctly. – Nate Norris Apr 05 '22 at 19:44
  • Agreed, please show us the code where you construct an instance of the `Example` class, surely the problem lies there. – CRice Apr 05 '22 at 20:37
  • 1
    Does this answer your question? [How to convert a plain object into an ES6 Map?](https://stackoverflow.com/questions/36644438/how-to-convert-a-plain-object-into-an-es6-map) – E_net4 Apr 06 '22 at 16:30

0 Answers0