I have now managed to get my previous json split by month and into a new JSON with help and guidance from members. I am now stuck on how to loop through it. All of the methods I used on the previous JSON are returning errors. Here is a shortened extract of the json:
"Dec '19":[
{
"hash":"820eRWiyq2Vy4Sk",
"arrival":"2019-12-01",
"departure":"2019-12-08",
"description":"",
"col1":"695",
"availability":"empty",
"availableRooms":"7",
"availableGuests":"16"
},
{
"hash":"8201ahPFdE28VUF",
"arrival":"2019-12-08",
"departure":"2019-12-15",
"description":"",
"col1":"695",
"availability":"empty",
"availableRooms":"7",
"availableGuests":"16"
},
"Jan '20":[
{
"hash":"820RDoYPKxtdkMK",
"arrival":"2020-01-05",
"departure":"2020-01-12",
"description":"",
"col1":"695",
"availability":"empty",
"availableRooms":"7",
"availableGuests":"16"
},
{
I think part of the problem is that the keys, in this case “Dec '19”, “Jan '20” etc are not known in advance as they are dynamically created from the first JSON (extract below):
myObjAll1= [
{
hash: "820eRWiyq2Vy4Sk",
arrival: "2019-12-01",
departure: "2019-12-08",
description: "",
col1: "695",
availability: "empty",
availableRooms: "7",
availableGuests: "16"
},
{
hash: "8201ahPFdE28VUF",
arrival: "2020-01-13",
departure: "2020-01-19",
description: "",
col1: "695",
availability: "empty",
availableRooms: "7",
availableGuests: "16"
},
returned via an API from a 3rd party company which have then been split by month using this:
function group_by_month(myObjAll1) {
var monthName = new Array();
monthName[0] = "Jan";
monthName[1] = "Feb";
monthName[2] = "Mar";
monthName[3] = "Apr";
monthName[4] = "May";
monthName[5] = "Jun";
monthName[6] = "Jul";
monthName[7] = "Aug";
monthName[8] = "Sep";
monthName[9] = "Oct";
monthName[10] = "Nov";
monthName[11] = "Dec";
var months = {}
for (var i=0; i<myObjAll1.length; i++) {
var obj = myObjAll1[i];
var date = new Date(obj.arrival);
var month = monthName[date.getMonth()] + " '" + [date.getYear().toString().substr(-2)];
// console.log(month);
if (months[month]) {
months[month].push(obj); // already have a list- append to it
}
else {
months[month] = [obj]; // no list for this month yet - create a new one
}
}
return months;
}