How to subtract the data from the adjacent items in a circular array

now there is an array: now we need to calculate the time difference between the first data and the second data, the time difference between the second and the third, and so on, and there are many pieces of data that are not fixed

.
 historyList: [{
        id:1,
        date:"2019-01-18 14:08"
      },{
        id:2,
        date:"2019-01-19 14:08"
      },{
        id:3,
        date:"2019-01-20 14:08"
      }]

I have no idea at this point: I don"t know how to subtract two adjacent items.

if (ctx.historyList.length > 1) {
            ctx.historyList.forEach((item, index) => {
              console.log(ctx.historyList[index].date)
            })
          }
May.06,2022

let result = [];
historyList.reduce((pre, curr) => {
    if (pre) {
        result.push(new Date(curr.date) - new Date(pre));
    }
    return curr.date;
}, 0);

result is millisecond interpolation. If you need other processing, do it before push. Such as converting to other formats


unix timestamp know.

< hr class= answer >
   

Menu