Sae*_*eid 7 typescript angular
我使用 Angular 6 + TypeScript 2.7.2 并且我有一个需要ISelect[]类型的组件:
export interface ISelect {
id: number;
title: string;
}
Run Code Online (Sandbox Code Playgroud)
所以每次我调用组件时,我都需要传递一个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;
}
Run Code Online (Sandbox Code Playgroud)
我需要像这个伪代码一样映射它。
myprojects: IProject[];
myselect: ISelect[] = myprojects.Foreach.Map<ISelect>(id: id, title: name);
Run Code Online (Sandbox Code Playgroud)
或者:
export interface IClient {
code: number,
fullname: string,
streetNumber: string,
streetName: string,
postalCode: string,
city: string,
isActive: boolean
}
Run Code Online (Sandbox Code Playgroud)
我需要这张地图:
myClients: IClient[];
myselect: ISelect[] = myClients.Foreach.Map<ISelect>(id: code, title: fullname);
Run Code Online (Sandbox Code Playgroud)
我怎么能做这个映射。我更喜欢避免在我的源数组中使用 for 循环。但是每次需要转换时我都可以有不同的映射。有什么建议吗?
像这样的东西会起作用
myselect: ISelect[] = this.myprojects.map(p => { return <ISelect>{ id: p.id, title: p.name } });
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9463 次 |
| 最近记录: |