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

CodePudding user response:

refer to the second floor weixin_50944805 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



 
//this
Var obj={' value ':' 1.77 ', 'time' : 'the 2020-12-09 14:32:00', 'name' : 'A'};
JsonHistory. Push (obj)
Var data=https://bbs.csdn.net/topics/jsonHistory.filter (v=> v.n ame==obj. The name), sort ((a, b)=> new Date (b.t ime) getTime () - the new Date (a.t ime) getTime ())
Var index=data. The map (v=& gt; V.t ime) indexOf (obj. Time)
Var num1=Math. Abs (new Date (data [index - 1]. The time). The getTime () - the new Date (obj. Time). GetTime ())
Var num2=Math. Abs (new Date (data [index + 1] time). The getTime () - the new Date (obj. Time). GetTime ())

Var res=num1 & gt; Num2? Data [index + 1] : data] [index - 1
The console. The log (res)//result



CodePudding user response:

reference weixin_50944805 reply: 3/f
Quote: refer to the second floor weixin_50944805 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



 
//this
Var obj={' value ':' 1.77 ', 'time' : 'the 2020-12-09 14:32:00', 'name' : 'A'};
JsonHistory. Push (obj)
Var data=https://bbs.csdn.net/topics/jsonHistory.filter (v=> v.n ame==obj. The name), sort ((a, b)=> new Date (b.t ime) getTime () - the new Date (a.t ime) getTime ())
Var index=data. The map (v=& gt; V.t ime) indexOf (obj. Time)
Var num1=Math. Abs (new Date (data [index - 1]. The time). The getTime () - the new Date (obj. Time). GetTime ())
Var num2=Math. Abs (new Date (data [index + 1] time). The getTime () - the new Date (obj. Time). GetTime ())

Var res=num1 & gt; Num2? Data [index + 1] : data] [index - 1
The console. The log (res)//result





When I run, error, Uncaught TypeError: always read the property 'time' of undefined
The at test. HTML: 27

CodePudding user response:

Need to judgment, possible data [index - 1] and data index + 1 does not exist,
There is no you is equal to another directly,
Because the index may be equal to zero or maximum

CodePudding user response:

 


//this
Var obj={' value ':' 1.77 ', 'time' : 'the 2020-12-09 14:32:00', 'name' : 'A'};
JsonHistory. Push (obj)
Var data=https://bbs.csdn.net/topics/jsonHistory.filter (v=> v.n ame==obj. The name), sort ((a, b)=> new Date (b.t ime) getTime () - the new Date (a.t ime) getTime ())
Var index=data. The map (v=& gt; V.t ime) indexOf (obj. Time)
Var res=obj
If (index==0) {//out could be the first
Res=data [index + 1]
} else if (index==data. The length - 1) {//out could be the last one
Res=data] [index - 1
} else if (index!=0 & amp; & Index!=data. The length - 1) {//ruled out is only own
Var num1=Math. Abs (new Date (data [index - 1]. The time). The getTime () - the new Date (obj. Time). GetTime ())
Var num2=Math. Abs (new Date (data [index + 1] time). The getTime () - the new Date (obj. Time). GetTime ())
Res=num1 & gt; Num2? Data [index + 1] : data] [index - 1
}


The console. The log (res)//result





CodePudding user response:

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull