【问题标题】:Javascript calculate time difference between timestamp within arrayJavascript计算数组内时间戳之间的时间差
【发布时间】:2021-07-26 09:36:41
【问题描述】:

如何计算时间戳数组中每个元素之间的差异?

我想用数组中每个元素之间的差异创建一个新数组。

我想要做的是将元素索引减去前一个元素之间的差值按顺序推送到新数组...

控制台中的输出几乎是数组元素的副本,不包括第一个元素...

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

请问我做错了什么?

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

【问题讨论】:

标签: javascript arrays


【解决方案1】:

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)

【讨论】:

  • 大部分答案都解决了我的问题,但由于我的代码范围,我更喜欢这个。感谢您的所有回答!
【解决方案2】:

timeArray 中的元素是简单的数字,它们没有time 属性,它绝对不是数组。

您也可以从减法中删除不必要的方括号。

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

【讨论】:

    【解决方案3】:

    你可以试试这个:

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

    【讨论】:

      【解决方案4】:

      为了完整性而减少

      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)

      【讨论】:

        【解决方案5】:

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

        【讨论】:

          【解决方案6】:

          你可以使用 map 来创建一个新的数组有区别:

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

          【讨论】:

          • 最后一个元素是NaN
          • 我删除了最后一个元素以防止出现 NaN
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-05-21
          • 1970-01-01
          • 2020-05-13
          相关资源
          最近更新 更多