'How to check object value unchanged based on timestamp?

I have an object with Timeseries and a value for showing a chart. The value updates often. I want to display the number of hours the value is unchanged.

For example: consider the below object, the value remains the same for some hours. I want to show how many hours it is unchanged. If any new value is different then want to update the hours to Zero.

obj = [{
  "value": 48.52062,
  "timestamp": 1652848223325
},
{
  "value": 48.52062,
  "timestamp": 1652838323281
},
{
  "value": 48.52062,
  "timestamp": 1652833823275
},
{
  "value": 51.71021,
  "timestamp": 1652830223280
},
{
  "value": 48.52062,
  "timestamp": 1652829323276
}]

I am trying to filter the object using obj.filter(e => e.value === 48.52062) and check that filtered object timestamps and calculate the hours of first object and last object timestamp

But, the filtered object returns even the old data, in this case, the data value is changed in between which is not a flatline. I just want to check only until the value last changed.

I am not sure, if this is the right way to do it. Here is the code:

var obj = [{
  "value": 48.52062,
  "timestamp": 1652848223325
},
{
  "value": 48.52062,
  "timestamp": 1652838323281
},
{
  "value": 48.52062,
  "timestamp": 1652833823275
},
{
  "value": 51.71021,
  "timestamp": 1652830223280
},
{
  "value": 48.52062,
  "timestamp": 1652829323276
}];

const x = obj.filter(e => e.value === 48.52062)
const timeStart = x[0].timestamp;
const timeEnd = x[x.length - 1].timestamp;


console.log(getHourDiff(timeStart, timeEnd));

function getHourDiff(start, end) {
   return Math.floor((end - start) / 3600000)
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source