简体   繁体   English

循环遍历数组并显示数据

[英]Looping through an array and displaying data

I am trying to loop through an array and display that.我试图遍历一个数组并显示它。 The problem I've got is that for some reason only the ID is being displayed.我遇到的问题是由于某种原因只显示了 ID。 I've gone through and debugged and can't understand why this doesn't work.我已经经历并调试过,但不明白为什么这不起作用。 All the data I want to process is available as I've checked through debugging and logging out the array.我想处理的所有数据都可用,因为我已经通过调试检查并注销了阵列。

Here is the HTML to display:这是要显示的 HTML:

<div class="col-xs-12 col-md-6 col-md-offset-3">
  <p *ngIf="carsArray.length < 1">No cars are available</p>
  <ul class="list-group" *ngIf="carsArray.length >= 1">
    <li class="list-group-item" *ngFor="let car of carsArray">
      <p>{{ car.id }}</p>
      <p>{{ car.type }}</p>
      <p>{{ car.manufacturer }}</p>
      <p>{{ car.colour }}</p>
    </li>
  </ul>
</div>

Here is my typescript that retrieves said data.这是我检索所述数据的打字稿。

fetchData() {
  this.http
    .get<{ [key: string]: car }>(
      'actual URL goes here'
    )
    .pipe(
      map((ResponseData) => {
        const carsArray: car[] = [];
        for (const key in ResponseData) {
          if (ResponseData.hasOwnProperty(key)) {
            carsArray.push({ ...ResponseData[key], id: key });
          }
        }
        return carsArray;
      })
    )
    .subscribe((post) => {
      this.carsArray = post;
      console.log(this.carsArray);
    });
}

Here is a what I'm currently getting:这是我目前得到的: https://i.imgur.com/hOCo7UE.png

Any help would be appreciated as I'm completely stumped!任何帮助将不胜感激,因为我完全被难住了!

The solution was to change my model file to have upper case types.解决方案是将我的模型文件更改为大写类型。 The data that was being submitted to the database was in uppercase...提交给数据库的数据是大写的...

export interface car {
  Manufacturer: string;
  Colour: string;
  Type: string;
  id?: string;
}

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

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