遍历Javascript中的字典列表

时间:2019-08-21 13:35:15

标签: javascript list loops dictionary

我试图遍历从我的python Flask api返回的字典列表,并将信息插入表的一行中,其中第一列是事件“类型”,第二列是“事件时间”。字典列表如下:

        { "type": "Creation Event", "time": event_results[0] },
        { "type": "Deletion Event", "time": event_results[1] },
        { "type": "Last Update Event", "time": event_results[2] }

这是我目前填写表格的方式

       return this.getData().then(event => {
           this.clear()

           this.addRow(['Event Type', 'Event Time'], 'th')

           if (!event) return;

           Object.keys(event).forEach(dict in list => {
               this.addRow([this.dict[type], dict[time]])
           })
       })
   }

对此有任何建议,将不胜感激。

1 个答案:

答案 0 :(得分:1)

您可以直接使用forEach遍历对象数组:

event.forEach(dict => this.addRow([obj.type, obj.time]));

您的代码:

return this.getData().then(event => {
  this.clear();

  this.addRow(["Event Type", "Event Time"], "th");

  if (!event) return;

  event.forEach(({type, time}) => this.addRow([type, time]));
});
相关问题