SOURCE

const records = [
    {
        time: 1622505600000,
        name: '2021/0601'
    },
    {
        time: 1622592000000,
        name: '2021/0602'
    },
    {
        time: 1625097600000,
        name: '2021/0701'
    },
    {
        time: 1625184000000,
        name: '2021/0702'
    },
    {
        time: 1627776000000,
        name: '2021/0801'
    },
    {
        time: 1627776000000,
        name: '2021/0801'
    },
    {
        time: 1640995200000,
        name: '2022/0101'
    },
    {
        time: 1643760000000,
        name: '2022/0202'
    }
]

const SortRecordsByDate = (records) => {
    const formatZero = (num) => {
        return num >= 10 ? num : "0" + num
    }

    const formatDate = (dateTime) => {
        const curDateYear = new Date(dateTime).getFullYear();
        const curDateMonth = new Date(dateTime).getMonth() + 1;
        return curDateYear + "-" + formatZero(curDateMonth)
    }

    const setDateRange = (date) => {
        const dateSplit = date.split('-');
        const year = +dateSplit[0];
        const month = +dateSplit[1];
        return {
            date: year + "年" + formatZero(month) + "月",
            startTime: new Date(year, month - 1).getTime(),
            endTime: new Date(year, month).getTime() - 1
        }
    }

    const newDate = Array.from(new Set(records.map(item => formatDate(item.time))))

    const sortRecords = newDate.map(item => {
        const dateRange = setDateRange(item);
        return {
            dateTitle: dateRange.date,
            data: records.filter(e => dateRange.startTime <= e.time && dateRange.endTime >= e.time)
        }
    })

    return sortRecords;
}

console.log(SortRecordsByDate(records));
console 命令行工具 X clear

                    
>
console