angular - 具有不同属性名称的 typescript map 类型

标签 angular typescript

我使用 Angular 6 + TypeScript 2.7.2,我有一个需要 ISelect[] 类型的组件:

export interface ISelect {
    id: number;
    title: string;
}

所以每次调用组件时,我都需要传递一个 ISelect[]。例如我有一个 IProject[] 并且我需要将它映射到 ISelect[] 在另一个例子中我有 IClient[] 然后我又需要将它映射到ISelect[],这里是IProject:

export interface IProject {
    id: number;
    name: string;
    code: string;
    clientId: number;
    status: number;
    level: number;
    masterKey: number;
    parentId?: number;
}

我需要像这个伪代码一样映射它。

 myprojects: IProject[];
 myselect: ISelect[] = myprojects.Foreach.Map<ISelect>(id: id, title: name);

或者:

export interface IClient {
    code: number,
    fullname: string,
    streetNumber: string,
    streetName: string,
    postalCode: string,
    city: string,
    isActive: boolean
}

我需要这张 map :

myClients: IClient[];
myselect: ISelect[] = myClients.Foreach.Map<ISelect>(id: code, title: fullname);

我怎么能做这个映射。我更愿意避免在我的源数组中使用 for 循环。但是每次我需要转换时我都可以有不同的映射。有什么建议吗?

最佳答案

像这样的东西会起作用

myselect: ISelect[] = this.myprojects.map(p => { return <ISelect>{ id: p.id, title: p.name } });

关于angular - 具有不同属性名称的 typescript map 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51406141/

相关文章:

javascript - Angular 2 - 停止点击事件传播

angular - 为什么使用 RxJS .asObservable() getter/factory 函数模式?

angular - 无法运行 Angular 应用程序,出现错误 : Schema validation failed

angular - 请问ng-bootstrap 支持bootstrap4 的新功能卡吗?

typescript - 键入具有任意数量的不同参数的函数

javascript - Express 和 Typescript - Error.stack 和 Error.status 属性不存在

javascript - 物化视差在 Angular2 应用程序中不显示图像

java - 具有 Angular URL 重写功能的嵌入式 Jetty

javascript - Angular 在 HttpClient.get header 中设置授权

javascript - 具有状态管理的 Angular 4 中的顺序(事务性)API 调用