【问题标题】:Angular 2 TypeScript using es6 Map type in Response DTO typeAngular 2 TypeScript 在响应 DTO 类型中使用 es6 Map 类型
【发布时间】:2017-03-21 07:00:23
【问题描述】:

我可以在 HTTP 响应 DTO 中使用 es6 Map 类型吗?

例如 Angular 2 请求:

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

}

还有一个 DTO

export class FoosWrapper{

    foos: Map<string, Foo[]>;

}

在 res.json() 之后,我收到的是简单对象而不是 Map。

我知道我必须自己将数据转换为地图,但最好的方法是什么? 迭代属性?

【问题讨论】:

标签: javascript angular typescript


【解决方案1】:

但是最好的方法是什么?

我会像在 JavaScript 中一样手动将其制作成地图:

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

How to convert a plain object into an ES6 Map?借用的函数

【讨论】:

    猜你喜欢
    • 2019-05-12
    • 1970-01-01
    • 2021-02-21
    • 2018-08-15
    • 1970-01-01
    • 2017-08-22
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    相关资源
    最近更新 更多