gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 17:51:15 25 4
gpt4 key购买 nike

我使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com