javascript - Ngfor 对象列表并从主数组中的对象之一内的数组再次循环

标签 javascript jquery html angular typescript

嗨,我有以下数据结构,

this._days=[

            {
               "day": "",
               "timing": {}
            },
            {
               "day": "Monday",
               "timing": [
                       {'9:00AM-10:00AM': {'subject': 'Physics','teacher': 'Amanda','Location': 'Room 05'}}, 
                       {'10:00AM-11:00AM': {'subject': 'Physics','teacher': 'Amanda','Location': 'Room 05'}}, 
                       {'11:00AM-12:00AM': {'subject': 'Physics','teacher': 'Amanda','Location': 'Room 05'}}, 
                       {'12:00AM-13:00AM': {'subject': 'Physics','teacher': 'Amanda','Location': 'Room 05'}}, 
                       {'13:00AM-14:00AM': {'subject': 'Physics','teacher': 'Amanda','Location': 'Room 05'}}
                    ]
            },
            {
               "day": "Tuesday",
               "timing": [
                       {'9:00AM-10:00AM': {'subject': 'Physics','teacher': 'Amanda','Location': 'Room 05'}}, 
                       {'10:00AM-11:00AM': {'subject': 'Physics','teacher': 'Amanda','Location': 'Room 05'}}, 
                       {'11:00AM-12:00AM': {'subject': 'Physics','teacher': 'Amanda','Location': 'Room 05'}}, 
                       {'12:00AM-13:00AM': {'subject': 'Physics','teacher': 'Amanda','Location': 'Room 05'}}, 
                       {'13:00AM-14:00AM': {'subject': 'Physics','teacher': 'Amanda','Location': 'Room 05'}}
                    ]
            },
...

我想显示像

这样的值

enter image description here

在我的 html 中我尝试过,

<table width="100%" align="center" height=100%;>
    <tr class="day">
        <th *ngFor="let row of _days">{{row.day}}</th>
    </tr>
    <tr class="time" *ngFor="let x of _days.timing">
        <th style="width:100px">{{_days.timing[x][key]}}</th>
            <td>{{_days.timing[x].value}}</td>
    </tr>
</table>

无论如何,我可以在我的html中实现这一点,这样我就可以获得如图所示的预期结果。提前谢谢大家。

最佳答案

首先,您需要根据时间过滤数据

var tabelRows = {};
var firstRow = [];
_days.forEach(function(v,i){
   v.timing.forEach(function(val,ind) {
      var row = Object.keys(val)[0];
      if(firstRow.indexOf(row) == -1) {
           firstRow.push(row);
           tabelRows[row] = [val[row]];
      } else {
      tabelRows[row].push(val[row]);
      }
   });
});

然后执行 forEach 循环,如下所示:

<tr class="time" *ngFor="let x of firstRow">
        <th style="width:100px">{{x}}</th>
            <td *ngFor="let y of tabelRows[x]">{{y.subject}}</td>
    </tr>

关于javascript - Ngfor 对象列表并从主数组中的对象之一内的数组再次循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43315535/

相关文章:

javascript - jquery 代码更改 html 最近不起作用

JavaScript:管理/添加动态内容和页面加载的最佳方法

javascript - 如何从嵌套结构中获取最高数字

javascript - 检测/预防隐私浏览引起的Firefox IndexedDB InvalidStateError

javascript - .height() 函数在设置为自动时显示错误值

javascript - jQuery.width() 返回百分比宽度而不是像素宽度

javascript - 模糊背景叠加图像

html - 突出显示链接到当前页面的 div

javascript - 有没有办法在IntelliJ中查找JavaScript的方法结构?

javascript - TryCatch 装饰器没有捕获错误