Home > Mobile >  javascript slice array of objects by date
javascript slice array of objects by date

Time:10-09

I have this 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'}]

the time is just a string, therefore I am adding the date to each object in order to have the .getDay() method from the Date.

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

I want it to be sliced by dates.

    [
      [
        {
            "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"
        }
      ]
    ]

CodePudding user response:

You could use reduce for this, and in each iteration check if the date part changed. If so, add a new subarray to the accumulator:

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

  • Related