简体   繁体   中英

Angular 2 TypeScript using es6 Map type in Response DTO type

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?

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM