0

I have a service that returns a map of key values pairs Map<String, List<String>>

{
    "code1": [
        "F10",
        "F11"
    ],
    "code2": [
        "F12",
        "F13"
    ]
}

I am trying to hold this response in angular app

getCodes(): Observable<Map<string, string[]>> {
   return this.http.get<Map<string, string[]>>(url, {headers})
        .pipe(map((response: Map<string, string[]>) => {
          return response;
        }));
    }

and after I subcribe to the observable and try to access the map with key, I am getting an error this.codes.get is not a function

Component:

codes = new Map<string, string[]>();

this.service.getCodes().subscribe(response => {
 this.codes = response;
 console.log('for code1', this.codes.get('code1'));
}

when I log the response, it is not looking like a map. Pls help

0

1 Answer 1

4

You are getting not a map, but a json.

You can type it correctly with:

return this.http.get<Record<string, string[]>>(url, {headers});
// note no "map(..)" is done. It is doing nothing so I omitted it

In order to convert it to Map you can use:

this.codes = new Map(Object.entries(response));
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you this helped

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.