0

Can I use es6 Map type in a HTTP Response DTO?

For instance Angular 2 request:

 public loadFoos(): Observable<FoosWrapper> {
    return this.http.get("/api/foo")
        .map(res => res.json());

}

And a DTO

export class FoosWrapper{

    foos: Map<string, Foo[]>;

}

After res.json() I am receiving simple object instead of Map.

I know I have to convert data myself into the Map, but what is the best approach for this? Iterating over the properties?

Simon
  • 997
  • 1
  • 15
  • 29
  • Possible duplicate of [ES6 Map in Typescript](http://stackoverflow.com/questions/30019542/es6-map-in-typescript) – Adam Mar 20 '17 at 22:55

1 Answers1

0

but what is the best approach for this?

I would just make it a map manually same as you would in JavaScript:

const buildMap = o => Object.keys(o).reduce((m, k) => m.set(k, o[k]), new Map());

Function borrowed from How to convert a plain object into an ES6 Map?

Community
  • 1
  • 1
basarat
  • 261,912
  • 58
  • 460
  • 511