Given the following exercise in 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'
})
}
})
so when attempting a GET call i.e. localhost:5000/weather/2015-09-01T16:00:00.000Z
What's the best way to loop through the array of objects recursively, retrieve an send a res.send()
only of the object that matches exactly the timestamp passed into req.param.id
?