gpt4 book ai didi

javascript - 如何打印出带有 json 的列表?

转载 作者:行者123 更新时间:2023-11-30 19:09:06 25 4
gpt4 key购买 nike

我需要将 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 });

最佳答案

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

res.json({attendees: response.rows.map(a=>a.attendee)});

关于javascript - 如何打印出带有 json 的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58685170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com