I am working with a nested JSON array. As shown below, The "booking_status" array has multiple arrays within, I would like to present on my table only the array that falls within the current time.
So, using the filter() method, I tried to compare the current time against each array check_in and check_out dates/time. If the current time is between these two values, only this array's details should be returned on my table else return the string "NOTHING". I wrote the codes below, but they return no values. I am not sure what I did wrong, please note the I am very new to Java and still learning from tutorials.
JSON Array:
[
{
"id": 2,
"item_name": "Rose Red",
"author": "Stephen King",
"booking_status": [
{
"check_in": "2021-09-22T08:27:00+04:00",
"check_out": "2021-09-23T08:27:00+04:00",
"status": "Currently Booked"
},
{
"check_in": "2021-09-25T08:37:00+04:00",
"check_out": "2021-09-26T08:37:00+04:00",
"status": "Currently Free"
},
{
"check_in": "2021-09-27T08:37:00+04:00",
"check_out": "2021-09-28T08:37:00+04:00",
"status": "Currently Free"
},
{
"check_in": "2021-09-22T08:27:00+04:00",
"check_out": "2021-09-23T08:27:00+04:00",
"status": "Currently Booked"
},
{
"check_in": "2021-09-25T08:37:00+04:00",
"check_out": "2021-09-26T08:37:00+04:00",
"status": "Currently Free"
}
]
}
]
Script:
<script>
$(document).ready(function () {
$.ajax({
url: 'http://localhost:8000/',
dataType: 'JSON',
success: function (data) {
var content = ''
data.forEach(item => {
const booking = item.booking_status;
let currentDatetime = new Date();
let FinalBooking = booking.filter(obj => obj.check_in <= currentDatetime < obj.check_out);
console.log(currentDatetime);
console.log(FinalBooking);
content += "<tr>><td>"
+ item.item_name + "</td><td>"
+ item.author+ "</td><td>"
+ item.booking_status.map(s => s.status).join('<br/>') + "</td></tr>"
})
$('#table_body').html(content);
}
})
});
</script>