How to realize array splitting gracefully

how should data summarized by time be converted to summary at regular intervals?

  let array = [{
    date: "2018-11-01",
    count: 10
  },{
    date: "2018-11-02",
    count: 10
  },{
    date: "2018-11-03",
    count: 20
  },{
    date: "2018-11-04",
    count: 20
  },
  ...];   
  
  => 
  
  let array = [{
    date: "2018-11-01~2018-11-02",
    count: 20
  },{
    date: "2018-11-03~2018-11-04",
    count: 40
  },
  ...]; 

how can this array split be elegant enough?

Nov.15,2021

// 
let intervalDate = 1

/**
 * 
 *
 * @param {Number} day
 */

function setIntervalDate(day) {
    intervalDate = day
}

/**
 * 
 *
 * @param {Date} date 
 * @param {Number} day 
 * @returns {Date} 
 */

function addDay(date, day) {
    const d = new Date()
    d.setTime(date.getTime() + day * 24 * 60 * 60 * 1000)
    return d
}

/**
 * 
 *
 * @param {Date} date 
 * @returns {String}   y-m-d
 */

function formatTime(date) {
    return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`
}

/**
 * 
 *
 * @param {Array<{date:String,count:Number}>} array 
 * @returns {Array<{date:String,count:Number}>} 
 */

function converArr(array) {
    return array
        //  date 
        .map(a => ({ ...a, date: new Date(a.date) }))
        // 
        .sort((a, b) => a - b)
        // 
        .reduce((prev, curr) => {
            const currD = curr.date
            
            // 
            let prevLast = prev[prev.length - 1]
            
            // ,
            // ,
            if (!prevLast || addDay(prevLast.sD, intervalDate) < currD) {
                prevLast = { sD: currD, eD: currD, count: curr.count }
                prev.push(prevLast)
            } else {
                // ,
                prevLast.eD = currD
                prevLast.count += curr.count
            }

            return prev
        }, [])
        // 
        .map(({ sD, eD, count }) => ({ date: `${formatTime(sD)}~${formatTime(eD)}`, count }))
}

module.exports = {
    converArr,
    setIntervalDate
}

Welcome to add.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1bf826b-320b1.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1bf826b-320b1.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?