簡體   English   中英

過濾 JSON 對象 Javascript/Node.js 的函數

[英]Function to filter JSON Object Javascript/Node.js

我有一個像這樣的 JSON 對象數組:

[{ 
  id: "1"
  times:{ 
          start: '2018-05-09T06:05:28.144Z',
          end: '2018-05-09T06:10:21.564Z' 
       },
},

{ 
  id: "2"
  times:{ 
          start: '2018-06-09T06:10:25.144Z',
          end: '2018-06-09T06:20:20.564Z' 
       },
}]

我想編寫一個函數,它只返回“開始”和“結束”時間戳之間的總分鍾數在特定范圍內的對象。

示例:返回“開始”和“結束”時間戳之間的總分鍾數在 3 到 5 分鍾范圍內的所有對象。

在此先感謝您的幫助。

應該是這樣:

 var timeArray = [{ id: "1", times: { start: '2018-05-09T06:06:21.564Z', end: '2018-05-09T06:10:21.564Z' } }, { id: "2", times: { start: '2018-06-09T06:10:25.144Z', end: '2018-06-09T06:20:20.564Z' } } ] function inRange(arr) { const max = 5 // 5 minutes const min = 3 // 3 minutes const filteredArray = arr.filter((obj) => { const range = (new Date(obj.times.end) - new Date(obj.times.start)) / 60000 // in minutes return range > min && range < max ? obj : '' }) return filteredArray } console.log(inRange(timeArray))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM