0

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

1

6 Answers 6

3

let 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]

let results = records.slice(1).map((e,i) => records[i+1] - records[i])

console.log(results)

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

1 Comment

The majority of the answers solved my issue but this one I prefer due to the scope of my code. Thank you for all your answers!
1

The elements in timeArray are simple numbers, they do not have a time property and it is definitely not an array.

You can remove the unnecessary square brackets from the subtraction too.

const 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(timeArray) {
      let newArray = [];
      for (let i = 1; i < timeArray.length; i++) {
        newArray.push(timeArray[i] - timeArray[i-1]);
      }
      return newArray;
}

console.log(timeInteraction(records));

Comments

0

You can try this:

function timeInteraction() {
    const nextRecords = records.slice(1);
    return nextRecords.map((next, prevId) => next - records[prevId]);
}

Comments

0

And a reduce for completeness' sake

const 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]

const newArr = records.reduce((acc,cur,i) => {
  if (i>0 && i<records.length) acc.push(records[i] - records[i-1])
  return acc;
},[])  

console.log(newArr)

Comments

0

let 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];
let newArray = [];
records.forEach((ele,i)=>{
  if(i < records.length - 1)
    newArray.push(records[i+1]-ele);
});

console.log(newArray);

Comments

0

You can use map to create a new array with the differences:

let 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]

let results = records.map((record, index) => {
    return records[index+1] - record
});

// Remove last element
results.pop();

console.log(results);

2 Comments

The last element is NaN
I removed the last element to prevent NaN

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.