Using a Date as a Hash Table Key
source link: https://www.codesd.com/item/using-a-date-as-a-hash-table-key.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Using a Date as a Hash Table Key
How can I create a hash table object in JavaSript and use a date as the key? So far I've got this:
var eventHash = {};
for (var i = 0, l = events.length; i < l; i += 1) {
eventHash[events[i].date.getTime()] = events[i];
}
And then when I want to find the event associated with today I would use this:
var event = eventHash[(new Date(2011, 04, 26, 0, 0, 0, 0)).getTime()];
Can anyone see any pitfalls with this solution, or have any suggestions for improvement?
Why wouldn't you just use an ISO8601 representation of the date, so the key would be like 20110426
. Creating a date object seems a bit inefficient.
It would also make debugging easier as the property names are more human readable, even if you add hhmmss also.
Related Articles
Using CLOS class instances as hash table keys?
scala speed when using the get () method on hash tables? (Are temporary Option () objects generated?)
What about the use of salt to avoid hash table collision?
Perl searches the data in the hash table faster
Reduce a log by date in a hash table
How to get the most recent date value from hash table
Ruby hash-table inserts a new element with the existing key
Copy a hash table to another hash table using c ++
Resizing the hash table: how do we do it without knowing the key?
Common Lisp: get all the keys of a given hash table as a list
Do we use hash tables in practice?
Suggestion for a large hash table (2 ^ 25 elements)
Find objects in a universal hash table?
Algorithm to find the intersection of two sets without using a data structure
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK