如何使用json打印列表?

我需要将json中的内容作为列表打印出来,这样看起来像:

{
  "attendees": [
    "Kevin Tyler","Syeda Shyra",]
}

但截至目前,我有:

{
    "attendees": [
        {
            "attendee": "Kevin Tyler"
        },{
            "attendee": "Syeda Shyra"
        }
    ]
}

我正在用它打印出来:

res.json({attendees: response.rows});

我的数据库在下面:

  attendee   |      workshop      
-------------+--------------------
 Ann Nowicki | React Fundamentals
 Ann Nowicki | TensorFlo
 Kevin Tyler | Biology 101
 Syeda Shyra | Biology 101

如何在不列出每个人的出席者的地方,而是在开始时只说一次参加者的地方?

如果需要,这里是我的更多代码:

 30 app.get("/api",async (req,res) => {
 31 
 32 
 33         try {
 34                 // if there is an argument
 35                 // else there isn't
 36 
 37                 // find attendee
 38                 const template = "SELECT attendee FROM people WHERE workshop =$1";
 39                 const response = await pool.query(template,[req.query.workshop]);
 40 
 41                 console.log(response);
 42                 // can print
 43                 if (req.query.workshop!=null) {
 44                         if (response.rowCount!=0) {
 45                                   res.json({attendees: response.rows});
 46                          } else {
 47                                 res.json({"error": "workshop not found"});
 48                         }
 49 
 50                 } else {
 51                         const resp = await pool.query("SELECT workshop FROM people");
 52                         res.json({workshops: resp.rows});
 53                 }
 54 
 55 
 56         } catch (err) {
 57                 console.error("whoops " + err);
 58                 res.json({status:"error"});
 59         }
 60 
 61 
 62 });
timberjack 回答:如何使用json打印列表?

您需要解开每个row的内容。使用map函数来实现。 Docs

res.json({attendees: response.rows.map(a=>a.attendee)});
本文链接:https://www.f2er.com/3169912.html

大家都在问