javascript - creating an array that contains missing objects -


i have object array. every gatetype:entry needs followed gatetype:exit object.

the problem entry object , exit objects missing/skipped. need create object array has missing objects.

            [{                 "datetime": "2016-01-28 15:18:26",                 "gatetype": "entry",             }, {                 "datetime": "2016-01-28 15:18:27",                 "gatetype": "exit",             }, {                 "datetime": "2016-01-28 15:19:26",                 "gatetype": "entry",             }, {                 "datetime": "2016-01-28 15:19:27",                 "gatetype": "exit",             }, {                 "datetime": "2016-01-28 15:20:26",                 "gatetype": "entry",             }, // here exit object missing above             {                 "datetime": "2016-01-28 15:21:25",                 "gatetype": "entry",             }, {                 "datetime": "2016-01-28 15:21:26",                 "gatetype": "exit",             }, {                 "datetime": "2016-01-28 15:22:25",                 "gatetype": "entry",              }, {                 "datetime": "2016-01-28 15:22:26",                 "gatetype": "exit",             },  // here entry object missing below             {                 "datetime": "2016-01-28 15:23:26",                 "gatetype": "exit",             }] 

each entry needs followed exit. can see entry or exit objects not exist.

the solution above example is:

            [{                 "datetime": "2016-01-28 15:20:26",                 "gatetype": "exit",             },{                 "datetime": "2016-01-28 15:23:26",                 "gatetype": "enty",             }] 

my solution doesn't work expected :

function detectmissingones(rows) {          var missings = [];         var current = null;          rows.foreach(function (key, index) {               if (index == 0) {                  if(key.gatetype == "exit"){                       var resultdate = moment(key.datetime);                     resultdate = resultdate.subtract(1, 'seconds').format("yyyy-mm-dd hh:mm:ss");                      missings.push({                         datetime: resultdate,                         gatenumber: key.gatenumber,                         gatetype: "entry",                         personid: key.personid,                         fix: 1                     });                  }                  current = key;                 return true;             }              if (current.gatetype == key.gatetype) {                  var type = key.gatetype == "entry" ? "exit" : "entry";                  var resultdate = moment(current.datetime);                 if(type == "entry"){                     resultdate = resultdate.subtract(1, 'seconds').format("yyyy-mm-dd hh:mm:ss");                 }else {                     resultdate = resultdate.add(1, 'seconds').format("yyyy-mm-dd hh:mm:ss");                 }                   missings.push({                     datetime: resultdate,                     gatenumber: current.gatenumber,                     gatetype: type,                     personid: current.personid,                     fix: 1                 });              }              current = key;          });          return missings;     } 

could me write algorithm or without lodash?

below code returns missing records. in question did not mention add/subtract second in missing records, in code present, did same.

i did not make use of library, manipulation seconds done via standard javascript functions, make bit longer.

this live snippet allows enter input array (json), , result of function (also json).

// main algorithm:  function detectmissingones(rows) {      var collect = rows.reduce(function (collect, curr) {          var expectedmove = collect.openentry ? "exit" : "entry";          var expectedperson = collect.openentry ? collect.openentry.personid : curr.personid;          console.log('expected move: ', expectedmove, ' person: ', expectedperson);          console.log('current move: ', curr.gatetype, ' person:', curr.personid);          if (expectedmove == curr.gatetype &&                   expectedperson == curr.personid) {              console.log('ok');              // ok, have expected gate type , personid.              // if entry, keep track of it, otherwise clear tracks              collect.openentry = collect.openentry ? null : curr; // toggle          } else {              console.log('mismatch');              // not ok, add orphans list              if (collect.openentry)       collect.orphans.push(collect.openentry);              if (curr.gatetype == 'exit') collect.orphans.push(curr);              // if entry, replace previous track              collect.openentry = curr.gatetype == 'entry' ? curr : null;          }          return collect;      }, {orphans: [], openentry: null}); // initial value of collect            // if last 1 "entry", add orphans list      if (collect.openentry) {          collect.orphans.push(collect.openentry);      }            // orphans: return missing counter part.      return collect.orphans.map(function(orphan) {          var mydate = new date(orphan.datetime.replace(/^(.{10}) (.{8})$/, '$1t$2z'))          // add/subtract 1 second:          mydate = new date(+mydate + 1000*(orphan.gatetype == "entry" ? 1 : -1));          return {              id:         orphan.id,              datetime:   mydate.toisostring().replace(/^(.{10})t(.{8}).*$/, '$1 $2'),              gatenumber: orphan.gatenumber,              gatetype:   orphan.gatetype == "entry" ? "exit" : "entry",              personid:   orphan.personid,              fix:        orphan.fix          };      });  }    // default input data:  var data = [    {      "id": 517,      "datetime": "2016-01-29 13:17:46",      "gatenumber": "192.168.1.206",      "gatetype": "exit",      "personid": 1,      "fix": 0    },    {      "id": 568,      "datetime": "2016-01-29 14:03:44",      "gatenumber": "192.168.1.203",      "gatetype": "entry",      "personid": 1,      "fix": 0    },    {      "id": 675,      "datetime": "2016-01-29 14:10:07",      "gatenumber": "192.168.1.203",      "gatetype": "entry",      "personid": 1,      "fix": 0    },    {      "id": 108,      "datetime": "2016-01-29 14:11:51",      "gatenumber": "192.168.1.205",      "gatetype": "entry",      "personid": 1,      "fix": 0    },    {      "id": 170,      "datetime": "2016-01-28 14:27:58",      "gatenumber": "192.168.1.206",      "gatetype": "exit",      "personid": 2,      "fix": 0    },    {      "id": 66,      "datetime": "2016-01-28 14:33:07",      "gatenumber": "192.168.1.200",      "gatetype": "exit",      "personid": 2,      "fix": 0    },    {      "id": 395,      "datetime": "2016-01-28 16:26:04",      "gatenumber": "192.168.1.206",      "gatetype": "exit",      "personid": 2,      "fix": 0    }  ];  // input controls:  var input = document.getelementbyid('input');  var output = document.getelementbyid('output');  var button = document.getelementbyid('button');    // default input value:  input.value = json.stringify(data, null, 2);    // execute process on button click:  button.onclick = function () {      var data = json.parse(input.value);      var missing = detectmissingones(data);      output.textcontent = json.stringify(missing, null, 2);  }
div { bottom: 0; right: 0; left: 0; top: 0; position: absolute }  table { with: 100%; height: 100% }  tr.page { with: 100%; height: 100% }  td { vertical-align: top; width: 50%}  textarea { min-width: 320px; width: 100%; height:100% }
<div class="big"><table border=1>      <tr><th>enter input (json):<button id="button">process</button></th>          <th>output</th></tr>      <tr class="page"><td><textarea id="input"></textarea></td>          <td><pre id="output"></pre></td></tr>  </table></div>

the function not alter passed argument (array). returned array lists missing objects. note need adapt code if have other properties in objects.


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

json - Gson().fromJson(jsonResult, Myobject.class) return values in 0's -