简体   繁体   English

Javascript:带有Timestamp作为键的数组

[英]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 基础知识: - 获取每个交易的输入日期和退出日期的停车操作列表 - 将所有这些日期记录到一个数组中,时间戳为关键字,+ = 1,如果输入日期,则= = 1表示退出日期 - 按日期排序 - 转到通过排序数组并添加到计数器,跟踪是否达到新的最大值

    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 再次感谢,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...in循环应该可以工作(但你应该使用.hasOwnProperty来保持理智)。

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: 你的NaN结果是因为你这样做:

result[es] += 1;

Since result[es] is undefined (because you never assigned it a value), undefined + 1 is NaN (not a number). 由于result[es]未定义(因为你从未为其赋值), undefined + 1NaN (不是数字)。

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;
      }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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