I have an output of a number of bookings which I have to use in a couple of ways.
I will use it to display a list of current bookings and also a list of past bookings, based on a "dateto" field in a timestamp format
Instead of making multiple queries ( WHERE dateto < currentdate, WHERE dateto > currentdate, etc. ) I just want to use the data I already have, but I am a bit unsure how to approach this.
I have this code.
let list = (req, res, next) => {
fetchAll(req.params.hid).then((bookings) => {
if (bookings.length > 0) {
(async () => {
res.render('profile/bookings/bookings.ejs', {
bookings: bookings,
moment: moment,
totalDays: await countBookingDays(bookings)
})
})()
} else {
res.send('No result')
}
}).catch((error) => {
res.status(500).send(error)
})
}
countBookingDays()
is just a function that sums all booking days in a year-to-date - not important for this particular issue.
I want to take bookings
and somehow mapping it to make a new array (pastBookings) with dateto < currentdate
So in my EJS file i can use it with something like this
res.render('profile/bookings/bookings.ejs', {
currentBookings: bookingsMappedToCurrent,
pastBookings: bookingsMappedToPast
moment: moment,
totalDays: await countBookingDays(bookings)
})
Man! It's easy to overcomplicate things
let currentBookings = bookings.filter(booking => booking.b_dateto > timeNow)
let pastBookings = bookings.filter(booking => booking.b_dateto < timeNow)
Works perfectly fine!
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments