2

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?

Jonca33
  • 3,333
  • 7
  • 25
  • 35
  • I don't believe it's quite a duplicate. I also need help figuring out the part of only sending the piece of corresponding data via `res.send()` – Jonca33 Mar 04 '19 at 04:39

0 Answers0