2017-05-08 41 views
0

我有一個數組,看起來像如何降低時間戳的數組元素的個數計數的數組中的特定時間範圍

[ 
    "2017-05-08T13:42:00.318Z", 
    "2017-05-08T13:42:05.590Z", 
    "2017-05-08T13:42:12.377Z", 
    "2017-05-08T13:42:20.830Z", 
    "2017-05-08T13:42:22.634Z", 
    "2017-05-08T13:42:25.249Z" 
] 

內,我想和計數組成的數組落得多少時間戳落入10秒的時間範圍。

因此,對於上述示例"2017-05-08T13:42:00.318Z""2017-05-08T13:42:05.590Z"將落入範圍2017-05-08T13:42:00.000Z2017-05-08T13:42:10.000Z

"2017-05-08T13:42:12.377Z"將落入2017-05-08T13:42:10.000Z2017-05-08T13:42:20.000Z的範圍內。

"2017-05-08T13:42:20.830Z","2017-05-08T13:42:22.634Z""2017-05-08T13:42:25.249Z"落入2017-05-08T13:42:20.000Z2017-05-08T13:42:30.000Z的範圍內。

我的目標是獲得這些範圍中每個範圍中的時間戳數量的數組數。對於上面的例子,結果將是:[2, 1, 3]

注意:我使用的是Typescript,因此使用ES6功能的答案將起作用。

+0

你嘗試過這麼遠嗎?提示:使用es6 [filter](https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)可能是一種方法。但如果沒關係,它會返回一個新數組。 – Dominik

回答

1

這可以工作,使用一些循環這我不特別驕傲的,但你可以改變如果你只是使用它,它會在以後佔用更多的數據由forEach自己調用的函數。

const timestampsToCheck = [ 
 
    "2017-05-08T13:42:00.318Z", 
 
    "2017-05-08T13:42:05.590Z", 
 
    "2017-05-08T13:42:12.377Z", 
 
    "2017-05-08T13:42:20.830Z", 
 
    "2017-05-08T13:42:22.634Z", 
 
    "2017-05-08T13:42:25.249Z" 
 
]; 
 

 
let nextTime = null; 
 
let outputTime = [0]; 
 

 
function getNextTime() { 
 
    nextTime.setSeconds(nextTime.getSeconds() + 10); 
 
} 
 

 
timestampsToCheck.forEach((element) => { 
 

 
    let thisTime = new Date(element); 
 
    let outputLast = outputTime.length - 1; 
 

 
    if (nextTime === null) { 
 
    nextTime = thisTime; 
 
    getNextTime(); 
 
    outputTime[outputLast]++; 
 
    } else if (thisTime > nextTime) { 
 
    getNextTime(); 
 
    while (thisTime > nextTime) { 
 
     getNextTime(); 
 
     outputTime.push(0); 
 
    } 
 
    outputTime.push(1); 
 
    } else { 
 
    outputTime[outputLast]++; 
 
    } 
 
}); 
 

 
console.log(outputTime);

3

您可以對日期進行切片並使用前一個日期進行檢查。

  value     slot   result 
------------------------ ------------------ --------- 
2017-05-08T13:42:00.318Z 2017-05-08T13:42:0 [1] 
2017-05-08T13:42:05.590Z 2017-05-08T13:42:0 [2] 
2017-05-08T13:42:12.377Z 2017-05-08T13:42:1 [2, 1] 
2017-05-08T13:42:20.830Z 2017-05-08T13:42:2 [2, 1, 1] 
2017-05-08T13:42:22.634Z 2017-05-08T13:42:2 [2, 1, 2] 
2017-05-08T13:42:25.249Z 2017-05-08T13:42:2 [2, 1, 3] 

var data = ["2017-05-08T13:42:00.318Z", "2017-05-08T13:42:05.590Z", "2017-05-08T13:42:12.377Z", "2017-05-08T13:42:20.830Z", "2017-05-08T13:42:22.634Z", "2017-05-08T13:42:25.249Z"], 
 
    count = data.reduce(function (r, a, i, aa) { 
 
     if (i && aa[i - 1].slice(0, 18) === a.slice(0, 18)) { 
 
      r[r.length - 1]++; 
 
     } else { 
 
      r.push(1); 
 
     } 
 
     return r; 
 
    }, []); 
 

 
console.log(count)

+0

這段代碼看起來不錯,但它嚴格硬連線到10秒 – Redu

+0

@Redu,10秒是問題的一部分。 –

0
const timestampsToCheck = [ 
    '2017-05-08T13:42:00.318Z', 
    '2017-05-08T13:42:05.590Z', 
    '2017-05-08T13:42:12.377Z', 
    '2017-05-08T13:42:20.830Z', 
    '2017-05-08T13:42:22.634Z', 
    '2017-05-08T13:42:25.249Z', 
]; 

const ranges = [ 
    ['2017-05-08T13:42:00.000Z', '2017-05-08T13:42:10.000Z'], 
    ['2017-05-08T13:42:10.000Z', '2017-05-08T13:42:20.000Z'], 
    ['2017-05-08T13:42:20.000Z', '2017-05-08T13:42:30.000Z'], 
]; 

const timestampsInRange = ranges.reduce((acc, range) => { 
    const count = timestampsToCheck.reduce((innerAcc, time) => { 
    return innerAcc + ((time > range[0] && time < range[1]) ? 1 : 0); 
    }, 0); 
    return acc.concat(count); 
}, []); 

console.log(timestampsInRange); 

工作小提琴:https://jsfiddle.net/9tjerwxa/

相關問題