I have an array of JSON Objects like this:
[{
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'
},
}]
I want to write a function say that will return only the objects that the total minutes between "start" and "end" timestamps are in a specific range.
example: return all objects that the total minutes between "start" and "end" timestamps is in the range 3 to 5 minutes.
Thanks in advance for your Help.
This should be it :
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))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.