Home > front end > Js, how to determine the closest two time
Js, how to determine the closest two time
Time:12-10
I have two json, a real-time data
JsonNow=[ {' value ':' 1.77 'and' time ':' the 2020-12-09 14:32:00 ', 'name' : 'A'}, {' value ':' 1.77 'and' time ':' the 2020-12-09 14:37:00 ', 'name', 'B'} ]
One is the historical data, the data is more, I only capture a fraction of, format is as follows:
JsonHistory=
{' value ':' 1.78 'and' time ':' the 2020-12-08 14:31:00 ', 'name' : 'A'}, {' value ':' 1.76 'and' time ':' the 2020-12-08 14:35:00 ', 'name' : 'A'}, {' value ':' 1.79 'and' time ':' the 2020-12-08 14:36:00 ', 'name' : 'A'}, {' value ':' 1.71 'and' time ':' the 2020-12-08 14:31:00 ', 'name', 'B'}, {' value ':' 1.66 'and' time ':' the 2020-12-08 14:35:00 ', 'name', 'B'}, {' value ':' 1.69 'and' time ':' the 2020-12-08 14:36:00 ', 'name', 'B'} ] Now I want to, according to the name in the jsonNow find time the most close to a data yesterday, Like right now, I want to find a
{' value ':' 1.78 'and' time ':' the 2020-12-08 14:31:00 ', 'name' : 'A'}, {' value ':' 1.69 'and' time ':' the 2020-12-08 14:36:00 ', 'name', 'B'} ] How do I want to achieve?
CodePudding user response:
1, the history data according to the first name group: https://blog.csdn.net/weixin_36339245/article/details/103522998 2, traverse now data, according to find the name in the history of the list, circulation, calculating minimum of two time the data (save two temporary variables, minTime - minimum time lag, the item - minimum time corresponding to the history data)
CodePudding user response:
Var val='A' Var data=https://bbs.csdn.net/topics/jsonHistory.filter (v=> v.n ame==val), sort ((a, b)=> new Date (b.t ime) getTime () - the new Date (a.t ime) getTime ()). The splice (0, 2) The console. The log (data)//result