循环遍历 Javascript 中的字典列表

Man*_*tty 1 javascript dictionary loops list

我试图循环遍历从 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] }
Run Code Online (Sandbox Code Playgroud)

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

       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]])
           })
       })
   }
Run Code Online (Sandbox Code Playgroud)

对此的任何建议都将不胜感激。

Tak*_*aki 5

您可以直接使用以下方法循环遍历对象数组forEach

event.forEach(dict => this.addRow([obj.type, obj.time]));
Run Code Online (Sandbox Code Playgroud)

你的代码:

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]));
});
Run Code Online (Sandbox Code Playgroud)