我正在try 循环访问一个array.问题是数据是嵌套的,并且在一个对象中有两个同名的array.我只想遍历一个array.ANGLE同时迭代这两个array.

我正在try 仅访问和打印street_group数组的第一个实例中的name.ANGING同时从street_group数组两个实例打印name.

这是数据


{
    "generated_at": "-",
    "schema": "-",
    "event": {
        "id": "-",
        "scheduled": "-",
        "value": false,
        "timing": {
            "type": "-",
        },
        "season": {
            "id": "-",
            "name": "-",
            "start_date": "-",
            "end_date": "-",
            "year": "-",
        },
        "cycle": {
            "id": "",
            "name": "-",
            "car": {
                "id": "-",
                "name": "-"
            },
            "category": {
                "id": "-",
                "name": "-"
            },
            "type": "-",
            "value": "-"
        },
        "useful": [{
            "id": "-",
            "name": "-",
            "value": "-",
            "value2": "-",
            "value3": "-",
            "value4": "-"
        }, {
            "id": "-",
            "name": "-",
            "value1": "-",
            "value2": "-",
            "value3": "-",
            "value4": "-"
        }],
        "venue": {
            "id": "-",
            "name": "-",
            "city_name": "-",
            "country_name": "-",
            "map_coordinates": "--",
            "country_code": "-",
            "values": [{
                "name": "-"
            }, {
                "name": "-"
            }]
        }
    },
    "section": [{
        "locale": "-",
        "value": {
            "id": "-",
            "name": "-",
            "country_code": "-"
        },
        "street_group": [{
            "id": "-",
            "name": "-",
            "type": "-",
        }, {
            "id": "-",
            "name": "-",
            "type": "-",
        }, {
            "id": "-",
            "name": "-",
            "type": "-",
        }
        ]
    }, {
        "locale": "-",
        "value": {
            "id": "-",
            "name": "-",
            "country_code": "-"
        },
        "street_group": [{
            "id": "-",
            "name": "-",
            "type": "-",
        }, {
            "id": "-",
            "name": "-",
            "type": "-",
        }, {
            "id": "-",
            "name": "-",
            "type": "-",
        }
        ]
}]
}

这是我的组件


export class LiveComponent implements OnInit {
 allData: {
    generated_at: string;
    section: {
      street_group: { name: string }[];
      locale: string }[];
    event: {
      id: string;
      useful: { name: string }[];
    };
  }[] | null = null;

    constructor(private http: HttpClient ) {
    }
    ngOnInit(){
this.http.get('http://api.redacted_for_privacy').subscribe(data => {this.allData = [data as any];});
    }
    }

这是我的Html


<mat-tab-group>

<mat-tab *ngFor="let data of allData" label="{{data.event.useful[0].name}}">

>! The part below is broken

<div class="content">
  <mat-list role="list" *ngFor="let section of data.section">
    <mat-list-item role="listitem" *ngFor="let street_group of section.street_group | slice:0:2">{{street_group.name}}</mat-list-item> 

  </mat-list>
  
</div>


    </mat-tab>
    
  </mat-tab-group> 


我try 向Section数组添加索引号,因为它是包含数组,但这 destruct 了代码.

<mat-list role="list" *ngFor="let section of data.section[0]">

其中,of表示此错误 Type '{ street_group: { name: string; }[]; }' is not assignable to type 'NgIterable | null | undefined'.ngtsc(2322)

我以为只会呈现第一个array.

推荐答案

如果我正确理解了您的问题,您不需要二维循环(Double*ngFor),而是第0个元素上的单个循环.

要打印SECTION ARRAY第一个元素中的所有名称:

  • 删除mat-list上的out*ngFor指令

仅循环通过mat-list-item内的节的第0个元素

您的HTML可能如下所示:

<mat-list role="list">
  <mat-list-item
    role="listitem"
     *ngFor="let street_group of data.section[0].street_group"
    >{{ street_grp.name }}</mat-list-item
  >
</mat-list>

注意,*ngFor="let street_grp of data.section[0].street_group"只迭代data.section[0]中的元素;

这里还应该提到的是,有时拼合嵌套数据可能很有用.这简化了数据的访问和操作.

Javascript相关问答推荐

我应该如何在此Angular 16应用程序中的方法中使用@ Hostspel?

单击树元素时阻止焦点事件触发?

访客柜台的风格React.js

如何expose 像React在onChange、onClick等中所做的那样的参数?

如何使用JavaScript将文本插入空div

我的服务工作器没有连接到我的Chrome扩展中的内容脚本.我该怎么解决这个问题?

使用JQuery单击元素从新弹出窗口获取值

如何从网站www.example.com获取表与Cheerio谷歌应用程序脚本

XSLT处理器未运行

使用Promise.All并发解决时,每个promise 的线性时间增加?

我正在建立一个基于文本的游戏在react ,我是从JS转换.我怎样才能使变量变呢?

如何迭代叔父元素的子HTML元素

如何在ASP.NET项目中使用Google Chart API JavaScript将二次轴线值格式化为百分比

在我的index.html页面上找不到我的Java脚本条形图

在开发期间,Web浏览器如何运行&qot;.jsx&qot;文件?

如何在Press上重新启动EXPO-AV视频?

更新Redux存储中的对象数组

如何用javascript更改元素的宽度和高度?

用于部分字符串的JavaScript数组搜索

通过跳过某些元素的对象进行映射