简体   繁体   中英

Javascript: Array with Timestamp as keys

I would like to have an array with timestamp as keys and numbers as values. This way I want to track how many cars have entered or left a parking lot as well as how many cars have parked in the parking lot simultaneously.

Basics: - Get the list of parking actions with enter date and exit date for each transaction - Get all those dates into an array with timestamp as key and += 1 if enter date and -=1 for exit date - Sort by date - Go through sorted array and add to a counter, track if new maximum is reached

    var result = [];
    var counter = 0;
    var max = 0;

    //SELECT enterTS, exitTS FROM parking;
    // validated, that works

    while (!rs.eof) {   
        var es = rs.fields(1).toString();
        var dd = rs.fields(2).toString();

        result[es] += 1;     //might happen at the same time with exit or other entries
        result[dd] -= 1;
        alert('Start' + es); //correct timestamp
        alert('Array' + result[es]); //shows NaN
    }

    result.sort();

    for (var key in result) {            
          counter += result[key];

          if(counter > max){
               max = counter;
          }
    }

Thanks for any help. I know this is not a working code snippet, but without the data connection this is tricky. I already tried the associative arrays, but was not able to understand how I can use in this example.

Thanks again, fj

Use an object, not an array.

var result = {};

Now to fill it you can just:

result[es] = result[es] + 1 || 1

And your for...in loop should work (but you should use .hasOwnProperty for sanity's sake).

for (var key in result) {        
    if (result.hasOwnProperty(key)) {    
        counter += result[key];

        if(counter > max){
             max = counter;
        }
    }
}

Your NaN result comes because you are doing this:

result[es] += 1;

Since result[es] is undefined (because you never assigned it a value), undefined + 1 is NaN (not a number).

You can't use a string as an index into an array; it amounts to using the array as an object. Even if you could, the logic is wrong because sorting an array sorts the values, not the indexes.

I suggest that you create an array of parking event objects and sort that using a custom comparison function. Something like this (untested):

var result = [];
var counter = 0;
var max = 0;

//SELECT enterTS, exitTS FROM parking;
// validated, that works

while (!rs.eof) {   
    var es = rs.fields(1).toString();
    var dd = rs.fields(2).toString(); // I'm assuming this is the exit time

    // create two events: one for entry and one for exit
    result.push({time: es, change: 1});
    result.push({time: dd, change: -1});
}

// sort based on event time
result.sort(function(a, b){ return a.time.localeCompare(b.time); });

// scan events, tracking current parking population
for (var key in result) {         
      counter += result[key].change;

      if(counter > max){
           max = counter;
      }
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM