javascript 按日期切片对象数组

javascript slice array of objects by date

我有这个数据

[{value: 29.3, type: 'temperature', unit: 'C', time: '2021-10-03T16:00:00.000Z'},
{value: 14, type: 'temperature', unit: 'C', time: '2021-10-03T17:00:00.000Z'},
{value: -4.1, type: 'temperature', unit: 'C', time: '2021-10-03T17:00:00.000Z'},
{value: 29.4, type: 'temperature', unit: 'C', time: '2021-10-03T17:00:00.000Z'},
{value: 6.1, type: 'temperature', unit: 'C', time: '2021-11-03T18:00:00.000Z'},
{value: 24.5, type: 'temperature', unit: 'C', time: '2021-11-03T18:00:00.000Z'},
{value: 8.3, type: 'temperature', unit: 'C', time: '2021-11-03T18:00:00.000Z'},
{value: 20, type: 'temperature', unit: 'C', time: '2021-12-03T19:00:00.000Z'},
{value: 20.6, type: 'temperature', unit: 'C', time: '2021-12-03T19:00:00.000Z'},
{value: 19.5, type: 'temperature', unit: 'C', time: '2021-12-03T20:00:00.000Z'}]

时间只是一个字符串,因此我将日期添加到每个对象,以便从日期获得 .getDay() 方法。

const temperatureData =
                data.filter(d => d.type === 'temperature')
                    .map(d => {
                        d.date = new Date(d.time)
                        return d
                    })

我希望它按日期切片。

    [
      [
        {
            "value": 29.3,
            "type": "temperature",
            "unit": "C",
            "time": "2021-10-03T16:00:00.000Z"
        },
        {
            "value": 14,
            "type": "temperature",
            "unit": "C",
            "time": "2021-10-03T17:00:00.000Z"
        },
        {
            "value": -4.1,
            "type": "temperature",
            "unit": "C",
            "time": "2021-10-03T17:00:00.000Z"
        },
        {
            "value": 29.4,
            "type": "temperature",
            "unit": "C",
            "time": "2021-10-03T17:00:00.000Z"
        }
      ],
      [
        {
            "value": 6.1,
            "type": "temperature",
            "unit": "C",
            "time": "2021-11-03T18:00:00.000Z"
        },
        {
            "value": 24.5,
            "type": "temperature",
            "unit": "C",
            "time": "2021-11-03T18:00:00.000Z"
        },
        {
            "value": 8.3,
            "type": "temperature",
            "unit": "C",
            "time": "2021-11-03T18:00:00.000Z"
        }
      ],
      [
        {
            "value": 20,
            "type": "temperature",
            "unit": "C",
            "time": "2021-12-03T19:00:00.000Z"
        },
        {
            "value": 20.6,
            "type": "temperature",
            "unit": "C",
            "time": "2021-12-03T19:00:00.000Z"
        },
        {
            "value": 19.5,
            "type": "temperature",
            "unit": "C",
            "time": "2021-12-03T20:00:00.000Z"
        }
      ]
    ]

您可以为此使用 reduce,并在每次迭代中检查日期部分是否更改。如果是,则向累加器添加一个新的子数组:

let data =[
    {value: 29.3, type: 'temperature', unit: 'C', time: '2021-10-03T16:00:00.000Z'},
    {value: 14, type: 'temperature', unit: 'C', time: '2021-10-03T17:00:00.000Z'},
    {value: -4.1, type: 'temperature', unit: 'C', time: '2021-10-03T17:00:00.000Z'},
    {value: 29.4, type: 'temperature', unit: 'C', time: '2021-10-03T17:00:00.000Z'},
    {value: 6.1, type: 'temperature', unit: 'C', time: '2021-11-03T18:00:00.000Z'},
    {value: 24.5, type: 'temperature', unit: 'C', time: '2021-11-03T18:00:00.000Z'},
    {value: 8.3, type: 'temperature', unit: 'C', time: '2021-11-03T18:00:00.000Z'},
    {value: 20, type: 'temperature', unit: 'C', time: '2021-12-03T19:00:00.000Z'},
    {value: 20.6, type: 'temperature', unit: 'C', time: '2021-12-03T19:00:00.000Z'},
    {value: 19.5, type: 'temperature', unit: 'C', time: '2021-12-03T20:00:00.000Z'}
];

let result = data.reduce((acc, item, i) => {
    if (i && item.time.replace(/T.*/, "") === data[i-1].time.replace(/T.*/, "")) {
        acc[acc.length-1].push(item);
    } else {
        acc.push([item]);
    }
    return acc;
}, []);

console.log(result);