简体   繁体   English

TypeError:数据不可迭代,在嵌套数组上使用 ngfor

[英]TypeError: data is not iterable , using ngfor on nested array

 rowData= [{ "label": "president", "contestants": [{ "name": "john key", "votes": 2 }, { "name": "john hi", "votes": 1 }, { "name": "john kl", "votes": 1 }] }, { "label": "organizer", "contestants": [{ "name": "michael skie", "votes": 3 }, { "name": "michael e", "votes": 1 }] }, { "label": "secretary", "contestants": [{ "name": "sampson eons", "votes": 2 }, { "name": "sampson mji", "votes": 2 }] }]
I have a rowData which looks like this and I want to dynamically create multiple bar charts with the results using NGFOR 我有一个看起来像这样的 rowData,我想使用 NGFOR 动态创建多个条形图和结果

 <div *ngIf="rowData.length >0"> <div *ngFor="let arr of rowData let i=index"> <div *ngFor="let elm of arr.contestants"> <ngx-charts-bar-vertical [view]="view" [scheme]="colorScheme" [results]="elm" [gradient]="gradient" [xAxis]="showXAxis" [yAxis]="showYAxis" [legend]="showLegend" [showXAxisLabel]="showXAxisLabel" [showYAxisLabel]="showYAxisLabel" [xAxisLabel]="xAxisLabel" [yAxisLabel]="yAxisLabel" (select)="onSelect($event)"> </ngx-charts-bar-vertical> </div> </div> </div>

I get an error that data is not iterable.我收到一个错误,即数据不可迭代。 Can someone help me有人能帮我吗

I solved it by rearranging the json我通过重新排列 json 解决了这个问题

 rowData = [ [{ "label": "president", "contestants": [{ "name": "john key", "votes": 2 }, { "name": "john hi", "votes": 1 }, { "name": "john kl", "votes": 1 }] }, { "label": "organizer", "contestants": [{ "name": "michael skie", "votes": 3 }, { "name": "michael e", "votes": 1 }] }, { "label": "secretary", "contestants": [{ "name": "sampson eons", "votes": 2 }, { "name": "sampson mji", "votes": 2 }] }] ]

 <div *ngIf="rowData.length >0"> <div *ngFor="let arr of rowData let i=index"> <div *ngFor="let elm of arr.contestants"> <ngx-charts-bar-vertical [view]="view" [scheme]="colorScheme" [results]="elm" [gradient]="gradient" [xAxis]="showXAxis" [yAxis]="showYAxis" [legend]="showLegend" [showXAxisLabel]="showXAxisLabel" [showYAxisLabel]="showYAxisLabel" [xAxisLabel]="xAxisLabel" [yAxisLabel]="yAxisLabel" (select)="onSelect($event)"> </ngx-charts-bar-vertical> </div> </div> </div>

I moved the results rowData[0];我移动了结果 rowData[0]; and it solved my problem它解决了我的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM