我正在尝试从地图转换可迭代的键,但是当我这样做时出现错误:
statistics.produceTypeData.keys不是函数
我正在关注以下问题的答案:How to convert Map keys to array?以使其正常运行。
当我尝试其他方法(使用Array.from(statistics.produceTypeData.keys())
)时,我得到了另一个错误,即:
类型'IterableIterator'不是数组类型。
在评论中我读到,在这种情况下,应使用扩展语法将Array.from()语句括起来,但是当我这样做时,我也会收到错误statistics.produceTypeData.keys is not a function
。
以下是代码段:
produceTypeLabels: ProduceType[];
this.produceTypeLabels = [...Array.from(statistics.produceTypeData.keys())];
ProduceType枚举:
export enum ProduceType {
FRUIT = "FRUIT",
VEGETABLE = "VEGETABLE",
HERB = "HERB",
NUT = "NUT",
SEED = "SEED",
MUSHROOM = "MUSHROOM",
CACTUS = "CACTUS"
}
统计对象来自get请求,看起来像这样:
export class DatabaseStatisticsDTO {
produceTypeData: Map<ProduceType,number>;
plantTypeData: Map<PlantType,number>;
lifeCycleData: Map<LifeCycleType,number>;
sunExposureData: Map<SunExposure,number>;
ripeColorData: Map<Color,number>;
frostResistanceData: Map<boolean,number>;
teaPlantData: Map<boolean,number>;
climbingPlantData: Map<boolean,number>;
pepperData: Map<boolean,number>;
}
有人知道我为什么会收到这些错误吗?我知道我可以简单地遍历可迭代的键并以这种方式填充数组,但是我真的很想使用传播语法或Array.from()函数来实现它。
谢谢
编辑:
这是调试器的堆栈跟踪,当我尝试对统计信息使用spread语法时。produceTypeData.keys()
RROR TypeError: statistics.produceTypeData.keys is not a function
at SafeSubscriber._next (database-statistics.component.ts:65)
at SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.__tryOrUnsub (Subscriber.js:195)
at SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.next (Subscriber.js:133)
at Subscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber._next (Subscriber.js:77)
at Subscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:54)
at MapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/map.js.MapSubscriber._next (map.js:41)
at MapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:54)
at FilterSubscriber.push../node_modules/rxjs/_esm5/internal/operators/filter.js.FilterSubscriber._next (filter.js:38)
at FilterSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:54)
at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber.notifyNext (mergeMap.js:79)
编辑2:更改了DatabaseStatisticsDTO,以便使用空的Map对象初始化映射字段,尽管在获取请求之后仍未定义这些字段。
export class DatabaseStatisticsDTO {
produceTypeData: Map<ProduceType,number> = new Map<ProduceType,number>();
plantTypeData: Map<PlantType,number> = new Map<PlantType,number>();
lifeCycleData: Map<LifeCycleType,number> = new Map<LifeCycleType,number>();
sunExposureData: Map<SunExposure,number> = new Map<SunExposure,number>();
ripeColorData: Map<Color,number> = new Map<Color,number>();
frostResistanceData: Map<boolean,number> = new Map<boolean,number>();
teaPlantData: Map<boolean,number> = new Map<boolean,number>();
climbingPlantData: Map<boolean,number> = new Map<boolean,number>();
pepperData: Map<boolean,number> = new Map<boolean,number>();
}
这是发出get请求的代码:
getStatistics():Observable<DatabaseStatisticsDTO>{
return this.http.get<DatabaseStatisticsDTO>(this.apiUrl + "/mod/getstatistics");
}
答案 0 :(得分:0)
好的,我解决了这个问题。问题是我期望的对象中包含地图对象,但是它们真正包含的是对象文字,需要先将其转换为Map对象。 现在的代码如下:
this.produceTypeLabels = Object.keys(statistics.produceTypeData);
this.produceTypeData = (<any>Object).values(statistics.produceTypeData).map(a => Number(a));
this.produceTypeBackgroundColors = ["#9400D3","#0000FF","#00FF00"];
我正在使用Object.keys从对象文字中获取键部分,并从(<any>Object).values
中获取值。我之所以使用(<any>Object)
而不是Object的原因是因为我没有安装ES2017库。我尝试这样做,但仍然收到警告,values()不是对象的功能。另外,也可以使用(<any>Object)