loop through list of dictionaries in Javascript - Stack Overflow

I'm trying to loop through a list of dicitionaries returned from my python Flask api and insert th

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745042084a4607870.html

相关推荐

  • loop through list of dictionaries in Javascript - Stack Overflow

    I'm trying to loop through a list of dicitionaries returned from my python Flask api and insert th

    8小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信