Reputation: 55
I have an array with the following time-series data. How can I split the array into intervals of 60 milliseconds(not greater than) and get the last array?
[
[1619932533,1781.85],
[1619932540,1781.72],
[1619932554,1781.99],
[1619932559,1783.84],
[1619932564,1783.02],
[1619932567,1780.38],
[1619932571,1780.52],
[1619932577,1780.38],
[1619932580,1783.07],
[1619932581,1782.17],
[1619932581,1782.17],
[1619932601,1782.95],
[1619932612,1782.7],
[1619932614,1782.82],
[1619932626,1782.82],
[1619932653,1784.12],
.....
]
I need the final data like this?
[
[1619932533,1781.85],
[1619932581,1782.17],
[1619932653,1784.12]
]
*split based on the first value 1619932533 and +60 and goes on....
Upvotes: 1
Views: 65
Reputation: 178011
Perhaps this? I only get two that are 60 milliseconds apart
const data = [
[1619932533,1781.85],
[1619932540,1781.72],
[1619932554,1781.99],
[1619932559,1783.84],
[1619932564,1783.02],
[1619932567,1780.38],
[1619932571,1780.52],
[1619932577,1780.38],
[1619932580,1783.07],
[1619932581,1782.17],
[1619932581,1782.17],
[1619932601,1782.95],
[1619932612,1782.7],
[1619932614,1782.82],
[1619932626,1782.82],
[1619932653,1784.12]
]
const times = data.reduce((acc,arr) => {
const [d,val] = arr;
if (acc.length===0) acc.push(arr)
else if (d - acc[acc.length-1][0] >= 60) acc.push(arr)
return acc
},[])
console.log(times)
Upvotes: 3
Reputation: 298
Please try to use this code.
var myObj, curr_time;
myObj = [[1619932533,1781.85],
[1619932540,1781.72],
[1619932554,1781.99],
[1619932559,1783.84],
[1619932564,1783.02],
[1619932567,1780.38],
[1619932571,1780.52],
[1619932577,1780.38],
[1619932580,1783.07],
[1619932581,1782.17],
[1619932581,1782.17],
[1619932601,1782.95],
[1619932612,1782.7],
[1619932614,1782.82],
[1619932626,1782.82],
[1619932653,1784.12]];
curr_time = myObj[0][0];
var result = [];
result.push(myObj[0])
for (i = 1; i < myObj.length; i++) {
if (myObj[i][0] - curr_time > 60) {
result.push(myObj[i]);
curr_time = myObj[i][0];
}
}
console.log('result', result);
Upvotes: 1