remove too close time elements from a json array -




i having array of json object contain timing , data.

basically, each element, contain timing, id , user below

[     {     "id": "abc",     "ts": "2017-08-17t20:42:12.557229",     "userid": "seb"     },     {     "id": "def",     "ts": "2017-08-17t20:42:52.724773",     "userid": "seb"     },     {     "id": "ghi",     "ts": "2017-08-17t20:42:53.724773",     "userid": "matt"     },     {      "id": "jkl",      "ts": "2017-08-17t20:44:50.557229",      "userid": "seb"     },     {      "id": "mno",      "ts": "2017-08-17t20:44:51.724773",      "userid": "seb"     },     {       "id": "pqr",       "ts": "2017-08-17t20:50:52.724773",       "userid": "seb"     } ] 

my goal remove object close each other if userid same. if time difference below 2 sec, remove element.

from list, should list

[     {     "id": "abc",     "ts": "2017-08-17t20:42:12.557229",     "userid": "seb"     },     {     "id": "def",     "ts": "2017-08-17t20:42:52.724773",     "userid": "seb"     },     {     "id": "ghi",     "ts": "2017-08-17t20:42:53.724773",     "userid": "matt"     },     {       "id": "pqr",       "ts": "2017-08-17t20:50:52.724773",       "userid": "seb"     } ] 

even if 2 objects user matt , seb close each other below 2seconds, have keep element it's not same user

"ts": "2017-08-17t20:42:52.724773" seb 

and

"ts": "2017-08-17t20:42:53.724773" matt 

any idea how code in ruby ? compared element n n-1 , delete n-1 if needed

require 'time'  result = []  timestamps = {}  data.each |item|   ts = timestamps[item['userid']]      if ts.nil? or time.parse(item['ts']) - time.parse(ts) > 2     result.push(item)     timestamps[item['userid']] = item['ts']   end end  puts result 




wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -