0

I have following JSON structure

{
"2019-03-27T21:00":23,
"2019-03-27T21:30":13,
"2019-03-27T21:48":20,
"2019-03-27T20:42":16
}

I would like to get this to a Typescript Map<Date, number>

I have tried declaring the map in my http call

this.http.get<Map<Date, number>>

Unfortunately this does not give me a map and calling response.values() is undefined on values()

How can I get my Json to a Map<Date, number>

1
  • but did you convert first object into map? Commented Mar 28, 2019 at 13:08

1 Answer 1

3

Saying that your response is of type Map<Date, number> won't typecast the data you receive, its just for Typescript intellisense that you are saying that the response is of type Map<Date, number> but your actual response is still the same.

Do this instead:

this.http.get<Map<Date, number>>(url).pipe(map(data) => {
    return new Map(Object.entries(data))
})
Sign up to request clarification or add additional context in comments.

2 Comments

I couldn't get your syntax to work. Not sure what map in map(data) is. But I got it working by getting an Object from the http call and creating new Map as you showed
@isADon map under pipe is an rxjs operator, you can import it like : import { map } from 'rxjs/operators'. We use "map" to modify the returning Observable, learnrxjs.io/operators/transformation/map.html

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.