Вот что я придумал. Возможно, вам удастся немного очистить его, но, похоже, он работает.
function mergeShutInDates(obj) {
let previousEndDate = null;
let shutInPeriod = null;
let theDayAfterThePreviousShutInEnd = null;
let currentStartDate = null;
let finalStartDate = null;
const finalShutInDates = [];
for (let i = 0; i < obj.shutInDates.length; i++) {
shutInPeriod = obj.shutInDates[i];
if (previousEndDate != null) {
currentStartDate = new Date(shutInPeriod.shutInStart);
theDayAfterThePreviousShutInEnd = new Date(previousEndDate);
theDayAfterThePreviousShutInEnd.setDate(new Date(previousEndDate).getDate() + 1);
if (sameDay(theDayAfterThePreviousShutInEnd, currentStartDate)) {
if (i == obj.shutInDates.length - 1) {
finalShutInDates.push({
shutInStart: finalStartDate,
shutInEnd: shutInPeriod.shutInEnd,
groupId: shutInPeriod.groupId,
shutInTypeId: shutInPeriod.shutInTypeId,
factorTypeId: shutInPeriod.factorTypeId
});
}
} else {
if (finalStartDate) {
finalShutInDates.push({
shutInStart: finalStartDate,
shutInEnd: previousEndDate,
groupId: shutInPeriod.groupId,
shutInTypeId: shutInPeriod.shutInTypeId,
factorTypeId: shutInPeriod.factorTypeId
});
finalStartDate = shutInPeriod.shutInStart;
}
if (i == obj.shutInDates.length - 1) {
finalShutInDates.push(shutInPeriod);
}
}
} else {
finalStartDate = shutInPeriod.shutInStart;
}
previousStartDate = shutInPeriod.shutInStart;
previousEndDate = shutInPeriod.shutInEnd;
}
obj.shutInDates = finalShutInDates;
}
function sameDay(date1, date2) {
return date1.getFullYear() === date2.getFullYear() &&
date1.getMonth() === date2.getMonth() &&
date1.getDate() === date2.getDate();
}
let obj1 = {
"entityId": 4307832,
"eventNumber": 0,
"scheduleId": 1,
"divisionId": 10,
"shutInDates": [{
"shutInStart": "08/01/2020",
"shutInEnd": "08/31/2020",
"groupId": 0,
"shutInTypeId": 7,
"factorTypeId": 0
},
{
"shutInStart": "10/01/2020",
"shutInEnd": "10/31/2020",
"groupId": 0,
"shutInTypeId": 5,
"factorTypeId": 0
},
{
"shutInStart": "01/01/2021",
"shutInEnd": "01/31/2021",
"groupId": 0,
"shutInTypeId": 5,
"factorTypeId": 0
},
{
"shutInStart": "11/01/2020",
"shutInEnd": "11/30/2020",
"groupId": 0,
"shutInTypeId": 5,
"factorTypeId": 0
},
{
"shutInStart": "12/01/2020",
"shutInEnd": "12/31/2020",
"groupId": 0,
"shutInTypeId": 5,
"factorTypeId": 0
}
]
}
let obj2 = {
"entityId": 4307832,
"eventNumber": 0,
"scheduleId": 1,
"divisionId": 10,
"shutInDates": [{
"shutInStart": "08/01/2020",
"shutInEnd": "08/31/2020",
"groupId": 0,
"shutInTypeId": 7,
"factorTypeId": 0
},
{
"shutInStart": "09/01/2020",
"shutInEnd": "09/30/2020",
"groupId": 0,
"shutInTypeId": 5,
"factorTypeId": 0
},
{
"shutInStart": "10/01/2020",
"shutInEnd": "10/31/2020",
"groupId": 0,
"shutInTypeId": 5,
"factorTypeId": 0
},
{
"shutInStart": "11/01/2020",
"shutInEnd": "11/30/2020",
"groupId": 0,
"shutInTypeId": 5,
"factorTypeId": 0
},
{
"shutInStart": "12/01/2020",
"shutInEnd": "12/31/2020",
"groupId": 0,
"shutInTypeId": 5,
"factorTypeId": 0
}
]
}
let obj3 = {
"entityId": 4307832,
"eventNumber": 0,
"scheduleId": 1,
"divisionId": 10,
"shutInDates": [{
"shutInStart": "08/01/2020",
"shutInEnd": "08/31/2020",
"groupId": 0,
"shutInTypeId": 7,
"factorTypeId": 0
},
{
"shutInStart": "10/01/2020",
"shutInEnd": "10/31/2020",
"groupId": 0,
"shutInTypeId": 5,
"factorTypeId": 0
},
{
"shutInStart": "01/01/2021",
"shutInEnd": "01/31/2021",
"groupId": 0,
"shutInTypeId": 5,
"factorTypeId": 0
},
{
"shutInStart": "11/01/2020",
"shutInEnd": "11/30/2020",
"groupId": 0,
"shutInTypeId": 5,
"factorTypeId": 0
},
{
"shutInStart": "03/01/2020",
"shutInEnd": "03/31/2020",
"groupId": 0,
"shutInTypeId": 5,
"factorTypeId": 0
}
]
}
let obj4 = {
"entityId": 4307832,
"eventNumber": 0,
"scheduleId": 1,
"divisionId": 10,
"shutInDates": []
}
let obj5 = {
"entityId": 4307832,
"eventNumber": 0,
"scheduleId": 1,
"divisionId": 10,
"shutInDates": [{
"shutInStart": "08/01/2020",
"shutInEnd": "08/31/2020",
"groupId": 0,
"shutInTypeId": 7,
"factorTypeId": 0
},
{
"shutInStart": "10/01/2020",
"shutInEnd": "10/31/2020",
"groupId": 0,
"shutInTypeId": 5,
"factorTypeId": 0
},
{
"shutInStart": "01/01/2021",
"shutInEnd": "01/31/2021",
"groupId": 0,
"shutInTypeId": 5,
"factorTypeId": 0
},
{
"shutInStart": "04/01/2020",
"shutInEnd": "04/30/2020",
"groupId": 0,
"shutInTypeId": 5,
"factorTypeId": 0
},
{
"shutInStart": "05/01/2020",
"shutInEnd": "05/30/2020",
"groupId": 0,
"shutInTypeId": 5,
"factorTypeId": 0
}
]
}
mergeShutInDates(obj1);
console.log(obj1);
mergeShutInDates(obj2);
console.log(obj2);
mergeShutInDates(obj3);
console.log(obj3);
mergeShutInDates(obj4);
console.log(obj4);
mergeShutInDates(obj5);
console.log(obj5);