0

当直接在邮递员中传递 id 以进行测试请求时,我试图访问英雄的特定详细信息。但是,它会输出所有内容,而不仅仅是输出我要求的物品/英雄。

我究竟做错了什么?

// overwatch hero detail
app.get("/hero/:id", (req, res) => {
  let detail = [
    {
        id: 1,
        real_name: "Hana Song",
        age: "19",
        nationality: "Korean",
        occupation: "Professional Gamer, Mech Pilot, Actress"
    },
    {
        id: 2,
        real_name: "Winston",
        age: "20",
        nationality: "",
        occupation: "Scientist"
    }
  ];
  res.json(detail)
});
4

2 回答 2

1

你需要一个过滤功能:

const id = req.params.id;
const result  = detail.filter((item)=> item.id === id)
return result[0]
于 2020-05-16T23:29:15.080 回答
1

您应该使用 find 函数在数组中查找特定对象

const result = detail.find(obj=>obj.id===id);
res.json(result)
于 2020-05-16T23:32:58.310 回答