I have a single JSON array that I would like to display on my user interface by only showing the last 5 JSON objects in reverse order. If the length of the array is less than 5, then all objects should be displayed in reverse order.
Currently, I can successfully display the last 5 objects if the length is greater than 5. However, if the array size is less than 5, my logic does not work as intended. Could someone assist me in fixing this issue?
for (var i = response.length - 1 ; i >= Math.max(response.length-5, 0) ; --i) {
var date = new Date(response[i].transactiondate);
var year = date.getFullYear();
var month = date.getMonth() + 1;
var dt = date.getDate();
response[i].transactiondate = dt + '-' + month + '-' + year;
outputdata += '<br>' + response[i].transactiondate + ' ' + response[i].amount + ' ' + response[i].description;
}