5

Using a Date as a Hash Table Key

 2 years ago
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

advertisements

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.

Tags javascript

Related Articles

Using CLOS class instances as hash table keys?

I have the following class: (defclass category () ((cat-channel-name :accessor cat-channel-name :initarg :cat-channel-name :initform "" :type string :documentation "Name of the channel of this category") (cat-min :accessor cat-min :ini

scala speed when using the get () method on hash tables? (Are temporary Option () objects generated?)

I am converting some code to Scala. It's code that sits in an inner loop with very large amounts of data so it needs to be fast, and it involves looking up keys in a hash table and computing probabilities. It needs to do different things depending on

What about the use of salt to avoid hash table collision?

If a key collides in a hash table, I want to find another position by salting the key, recursively, until I find a free position (always using the same salt). For example: "bee" and "ant" hash to 7 I insert "bee" in the table

Perl searches the data in the hash table faster

I use code like this to find data values for my calculations: sub get_data { $x =0 if($_[1] eq "A"); #get column number by name $data{'A'}= [2.00000, 0.15000, -0.00143, 33.51030, 0.77, 1, 0, 12]; return $data{$_[0]}[$x]; } Data is stored like th

Reduce a log by date in a hash table

A local variable named log contains an array of hashes with timestamped events like so: log = [ {time: 201201, x: 2}, {time: 201201, y: 7}, {time: 201201, z: 2}, {time: 201202, a: 3}, {time: 201202, b: 4}, {time: 201202, c: 0} ] I need to collapse th

How to get the most recent date value from hash table

I have an array of hashes like this: hashes = [{date: Date.today, value: 1}, {date: Date.today - 1.day, value: 2}, {date: Date.today - 2.day, value: 3}, {date: Date.today + 1.day, value: 4} ] And I want to get most recent date. I could get by: hashes

Ruby hash-table inserts a new element with the existing key

Say I have a array of words, different words might have different lengths, and I want to organise them by length. So later I can access all words share with same length by giving a parameter of length. words = Array.new() #fill words by reading file

Copy a hash table to another hash table using c ++

I am starting with c++ and need to know, what should be the approach to copy one hashtable to another hashtable in C++? We can easily do this in java using: HashMap copyOfOriginal=new HashMap(original); But what about C++? How should I go about it? U

Resizing the hash table: how do we do it without knowing the key?

When we store only the resultant data for a key in a hashtable, how do I perform hashtable resizing? One example I can think of is to store username & password combinations. For the sake of privacy, we'd only store passwords (there can be many other

Common Lisp: get all the keys of a given hash table as a list

I wonder if there is a less verbose way than using a loop. Anyway this works for me in CLISP: (loop for key being the hash-keys of *my-hash* collect key) I've seen others using maphash, but that involves accumulating each key into a list. Apart from

Do we use hash tables in practice?

I just read about Hash tables and am curious if we use it in practise because if I write a program that stores data in a Hash table, the storage will only be temporary. So, why not use a database to store it ? In other words, what kinds of real world

Suggestion for a large hash table (2 ^ 25 elements)

I want to write a birthday attack program in Haskell for a variant of SHA1 which only produces only a 50 bit hash. To do this I need a hash table capable of storing approx. 2^25 entries. The keys in this map will be Int64 and the values will be short

Find objects in a universal hash table?

If items are organized randomly, how does the table know where to start looking? In a non-random table items are organized according to some characteristic. (i.e. name). So if the table needs to look up some arbitrary information about "John", i

Algorithm to find the intersection of two sets without using a data structure

I would like to know the algorithm to determine the intersection of two arrays of equal elements (say, integer) without using any external data structure (like hash table) efficiently (O(nlogn))?sort, then iterate using an iterator to each element ar

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK