Javascript 如何基于索引访问Angular 4中数组数组中的元素?

Javascript 如何基于索引访问Angular 4中数组数组中的元素?,javascript,arrays,angular,Javascript,Arrays,Angular,我只想在Angular 4表格中迭代电视广告1的第一个数组元素,从2017年2月10日到2018年29月1日。但我面临着挑战,因为它正在迭代所有电视广告1、2、3、4表格的所有元素 请在下面找到我的代码: JSON: HTML: <thead> <tr class="black-muted-bg" *ngFor="let item of calendarTable" > <t

我只想在Angular 4表格中迭代电视广告1的第一个数组元素,从2017年2月10日到2018年29月1日。但我面临着挑战,因为它正在迭代所有电视广告1、2、3、4表格的所有元素

请在下面找到我的代码: JSON:

HTML:

  <thead>
                    <tr class="black-muted-bg" *ngFor="let item of calendarTable" >
                        <th class="align-right" *ngFor="let data of item.weeks">{{data.period}}</th>

                    </tr>
                </thead>

请在这方面帮助我

在循环之前提取数组并将其保存在变量中。由于calendarTable是对象的数组,calendarTable[0]将给出第一个对象和calendarTable[0]。weeks将给出该对象的week键的值

var toLoop = calendarTable[0].weeks;
只需将[0]添加到您的日历表中。。。检查下面的代码

<thead>
                <tr class="black-muted-bg" *ngFor="let item of calendarTable[0]" >
                    <th class="align-right" *ngFor="let data of item.weeks">{{data.period}}</th>

                </tr>
            </thead>
或者你可以这样走

<thead>
                <tr class="black-muted-bg"  >
                    <th class="align-right" *ngFor="let data of calendarTable[0].weeks">{{data.period}}</th>

                </tr>
            </thead>

获取arraycalendarTable的第一项,并循环数周

<thead>
   <tr class="black-muted-bg">
       <th class="align-right" *ngFor="let data of calendarTable[0].weeks">
           {{data.period}}
       </th>
   </tr>
</thead>

如果需要动态选择表的每个对象,可以执行以下操作:

为您服务:

专用日历表=[ //这是你已经有的桌子 ]; getAdsNames:string[]{ 返回Array.fromthis.calendarTable,ad=>ad.name; } getAdadName:字符串:任意{ 返回this.calendarTable.filterad:=>ad.name===adName[0]; } 在您的组件中:

可用字段:字符串[]; activeAd:任何; 恩戈尼特{ this.availableAds=this.myService.getAdsNames; //从列表中加载第一个广告 this.activeAd=this.setActiveAdthis.availableAds[0]; } setActiveAdadName:字符串{ this.activeAd=this.myService.getAdadName; } 在模板中:

{{week.period} {{ad}} 如果您不需要动态更改ads,您仍然应该使用服务来处理您的数据,并在需要时简单地请求值,例如在组件的init上-在ngOnInit方法中,并使其可用于模板

注意:你应该避免使用任何广告,因为我曾经简化过这个例子,并为你的广告创建了一个模型


干杯

直接使用第一个元素,然后在几周内重复。太棒了。谢谢。另外,您能否建议如何在表中添加分页。当然。。。在这里检查一下。。。
<thead>
   <tr class="black-muted-bg">
       <th class="align-right" *ngFor="let data of calendarTable[0].weeks">
           {{data.period}}
       </th>
   </tr>
</thead>