How can I calculate the difference between each element within a timestamp array?
I want to create a new array with the diffence between each element of the array.
What I am trying to do is to push to the new array the difference calculated between the element index minus the previous element, in sequence...
The output in the console was almost a copy of the elements of the array, excluding the first element...
(46) [1612271489789, 1612271498250, 1612271498756, 1612271499731, 1612271507831, 1612271508337, 1612271509048, 1612271511891, 1612271511895, 1612271512084, 1612271519936, 1612271520438, 1612271521713, 1612271525260, 1612271525264, 1612271527431, 1612271544137, 1612271544640, 1612271546389, 1612271551144, 1612271551646, 1612271553157, 1612271553163, 1612271557885, 1612271558028, 1612271558032, 1612271559399, 1612271559402, 1612271564461, 1612271564566, 1612271564570, 1612271564572, 1612271564577, 1612271567860, 1612271567863, 1612271568180, 1612271573603, 1612271573607, 1612271601243, 1612271601355, 1612271603883, 1612271604061, 1612271608817, 1612271608930, 1612271612965, 1612271613999]
What am I doing wrong, please?
records = [1612271438035, 1612271489789, 1612271498250, 1612271498756, 1612271499731, 1612271507831, 1612271508337, 1612271509048, 1612271511891, 1612271511895, 1612271512084, 1612271519936, 1612271520438, 1612271521713, 1612271525260, 1612271525264, 1612271527431, 1612271544137, 1612271544640, 1612271546389, 1612271551144, 1612271551646, 1612271553157, 1612271553163, 1612271557885, 1612271558028, 1612271558032, 1612271559399, 1612271559402, 1612271564461, 1612271564566, 1612271564570, 1612271564572, 1612271564577, 1612271567860, 1612271567863, 1612271568180, 1612271573603, 1612271573607, 1612271601243, 1612271601355, 1612271603883, 1612271604061, 1612271608817, 1612271608930, 1612271612965, 1612271613999];
function timeInteraction() {
let timeArray = [records];
let newArray = [];
for (let i = 1; i < timeArray.length; i++) {
var time = [timeArray[i].time];
console.log(time);
newArray.push(timeArray[i].time - timeArray[i].time[i - 1]);
}
console.log(newArray);
}
timeInteraction();
[<>]snippet editor and produce a minimal reproducible example