Как построить массив по датам - PullRequest
0 голосов
/ 06 мая 2020

const DATA = [{
    "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": "11/01/2020",
        "shutInEnd": "11/30/2020",
        "groupId": 0,
        "shutInTypeId": 5,
        "factorTypeId": 0
      }
    ]
  },
  {
    "entityId": 33756087,
    "eventNumber": 0,
    "scheduleId": 1,
    "divisionId": 10,
    "shutInDates": [{
        "shutInStart": "06/01/2020",
        "shutInEnd": "06/30/2020",
        "groupId": 0,
        "shutInTypeId": 7,
        "factorTypeId": 0
      },
      {
        "shutInStart": "07/01/2020",
        "shutInEnd": "07/31/2020",
        "groupId": 0,
        "shutInTypeId": 7,
        "factorTypeId": 0
      }
    ]
  },
  {
    "entityId": 34543593,
    "eventNumber": 0,
    "scheduleId": 1,
    "divisionId": 30,
    "shutInDates": [{
      "shutInStart": "10/01/2020",
      "shutInEnd": "10/31/2020",
      "groupId": 0,
      "shutInTypeId": 7,
      "factorTypeId": 0
    }]
  },
  {
    "entityId": 34543634,
    "eventNumber": 0,
    "scheduleId": 1,
    "divisionId": 30,
    "shutInDates": [{
      "shutInStart": "10/01/2020",
      "shutInEnd": "10/31/2020",
      "groupId": 0,
      "shutInTypeId": 7,
      "factorTypeId": 0
    }]
  },
  {
    "entityId": 34544797,
    "eventNumber": 0,
    "scheduleId": 1,
    "divisionId": 30,
    "shutInDates": [{
      "shutInStart": "10/01/2020",
      "shutInEnd": "10/31/2020",
      "groupId": 0,
      "shutInTypeId": 7,
      "factorTypeId": 0
    }]
  }
]

const groupConsecutive = (monthsNumbers) => monthsNumbers.reduce((r, n) => {
  const lastSubArray = r[r.length - 1];
  if (!lastSubArray || lastSubArray[lastSubArray.length - 1] !== n - 1) r.push([]);
  r[r.length - 1].push(n);
  return r;
}, []);


const result = DATA.map(x => {
  const shutInDates = [];
  if (x.shutInDates.length === 1) {
    shutInDates.push(x.shutInDates);
  } else {
    const monthsNumbers = [];
    const start = [];
    const end = [];
    let obj = {};
    x.shutInDates.forEach(s => {
      obj = {
        ...obj
      };
      obj = s;
      monthsNumbers.push(new Date(s.shutInStart).getMonth())
      start.push(new Date(s.shutInStart))
      end.push(new Date(s.shutInEnd))
    })

    const differenceAry = monthsNumbers.slice(1).map((n, i) => n - monthsNumbers[i])
    const isConsecutive = differenceAry.every(value => value == 1)

    if (isConsecutive) {
      const sortedStart = start.sort((a, b) => a - b)
      const sortedEnd = end.sort((a, b) => b - a)
      obj.shutInStart = sortedStart[0]
      obj.shutInEnd = sortedEnd[0]
      shutInDates.push(obj);
    } else {
      const consecutiveMonths = groupConsecutive(monthsNumbers);
      const currentYear = new Date().getFullYear()
      consecutiveMonths.forEach((arry, i) => {
        arry.forEach(m => {
          obj = {
            ...obj
          };
          obj.shutInStart = new Date(currentYear, m, 1);
          obj.shutInEnd = new Date(currentYear, m + 1, 0);
          shutInDates.push(obj)
        })
      });
    }
  }
  x.shutInDates = shutInDates;
  return x;
})

console.log(result);

Если даты расположены в последовательном порядке, мне нужно взять первый месяц в качестве даты начала и взять последний месяц в качестве даты окончания. Итак, здесь, в этом фрагменте, у меня есть октябрь и ноябрь как последовательные даты, поэтому мне нужно их объединить. Я почти у цели, просто нужна помощь, чтобы закончить. Примечание. * Мне нужно учитывать несколько месяцев подряд. Это может быть январь-март, май, июль-сентябрь и т. Д. c ...

Этот первый объект выглядит так:

    {
    "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": "11/01/2020",
        "shutInEnd": "11/30/2020",
        "groupId": 0,
        "shutInTypeId": 5,
        "factorTypeId": 0
      }
    ]
  }

Желаемый результат должен выглядеть так :

    {
    "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": "11/30/2020",
        "groupId": 0,
        "shutInTypeId": 5,
        "factorTypeId": 0
      }
    ]
  }

1 Ответ

0 голосов
/ 07 мая 2020

Вот что я придумал. Возможно, вам удастся немного очистить его, но, похоже, он работает.

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);
...