1

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....

3
  • Please add the code you've tried. Add a loop, compare the current value with previous item in the loop and last item in the output. Commented May 2, 2021 at 11:58
  • First time is 17:58:52.533 and last time is 17:58:52.653 - there are not 60 seconds between them. Do you mean milliseconds? Commented May 2, 2021 at 12:02
  • @mplungjan 1619932533 + 60 is 1619932593. The largest number in that range is 1619932581. Commented May 2, 2021 at 12:10

2 Answers 2

3

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)

Sign up to request clarification or add additional context in comments.

4 Comments

If d - acc[acc.length-1][0] >= 60, you need to get data[i-1] and push that to the output (You'd have to the third i parameter to reduce callback)
Why? acc[acc.length-1] contains the last saved value from data
The condition is correct. The item that needs to be pushed to acc is the previous item in the data array.
I am sorry, I do not get it. Please post your own answer to show the correct values according to you. I did just wake up so perhaps I’m too sleepy
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.