admin管理员组

文章数量:1410697

I'm trying to loop through a list of dicitionaries returned from my python Flask api and insert the information into a row in a table where the first column is the Event "type" and the second column is "Event Time". The list of dicitonaries looks like this:

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

Here is my current way of filling in the table

       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]])
           })
       })
   }

Any advice on this is greatly appreciated.

I'm trying to loop through a list of dicitionaries returned from my python Flask api and insert the information into a row in a table where the first column is the Event "type" and the second column is "Event Time". The list of dicitonaries looks like this:

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

Here is my current way of filling in the table

       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]])
           })
       })
   }

Any advice on this is greatly appreciated.

Share Improve this question edited Aug 21, 2019 at 13:37 Andrej Kesely 196k15 gold badges58 silver badges103 bronze badges asked Aug 21, 2019 at 13:35 mangokittymangokitty 2,4393 gold badges15 silver badges20 bronze badges 1
  • is event the array of objects ? – Taki Commented Aug 21, 2019 at 13:40
Add a ment  | 

1 Answer 1

Reset to default 5

You can loop through the array of objects directly using forEach:

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

your code :

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]));
});

本文标签: loop through list of dictionaries in JavascriptStack Overflow