在Node.js中进行以下练习
// SEND response
router.get('/:weatherId', (req, res, next) => {
const id = req.params.weatherId
const request = [
{
"timestamp":"2015-09-01T16:00:00.000Z",
"temperature": 27.1,
"dewPoint": 16.7,
"precipitation": 0
},
{
"timestamp":"2016-09-01T16:00:00.000Z",
"temperature": 27.1,
"dewPoint": 16.7,
"precipitation": 0
},
{
"timestamp":"2015-09-01T16:00:00.000Z",
"temperature": 27.1,
"dewPoint": 16.7,
"precipitation": 0
},
{
"timestamp":"2014-09-01T16:00:00.000Z",
"temperature": 27.1,
"dewPoint": 16.7,
"precipitation": 0
},
]
if (id === /* exact timestamp */) {
res.send(request) /* request should send only the object that matches the timestamp */
} else {
res.status(404).json({
message: 'Ouch Weather Not found'
})
}
})
因此,在尝试进行GET调用时,即localhost:5000/weather/2015-09-01T16:00:00.000Z
递归遍历对象数组,检索仅发送与res.send()
中传递的时间戳完全匹配的对象的req.param.id
的最佳方法是什么?