By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,700 Members | 1,579 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,700 IT Pros & Developers. It's quick & easy.

Problems with plus or minus sign as key in a javascript hash?

P: n/a
Hi all,

Has anyone ever seen the following Javascript problem (in IE). I'm
trying to set up a hash of rating values to integers (which I'll then
use to sort an array of ratings). It looks like this:

var ratingsTable = new Object();
ratingsTable["AAA"] = 0;
ratingsTable["AA+"] = 1;
ratingsTable["AA"] = 2;
ratingsTable["AA-"] = 3;
ratingsTable["A+"] = 4;
ratingsTable["A"] = 5;
ratingsTable["A-"] = 6;
....

However, any values that contain the "+" or "-" don't get added. I up
with a hash containing
AAA = 0, AA = 2, A = 5

This is something I've never come across in javascript before, but then
again I've never had to do it. A colleague has suggested preprocessing
the strings coming in for comparison so that "AA+" becomes "AAPlus"
That'll certainly work but is somewhat of a kluge. Is there some way
to escape the + or - sign so that these work as keys that are strings?
Am I going about this in the completely wrong way? Thanks, any help is
greatly appreciated!

Regards,
Jason

Nov 24 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a

jm********@earthlink.net wrote:
However, any values that contain the "+" or "-" don't get added. I up
with a hash containing
AAA = 0, AA = 2, A = 5

I don't see this. Tested the following code:

window.onload = function() {
var k = {};
k['aa+'] = 1;
k['aa'] = 2;
k['aa-'] = 3;
s = ''
for (var p in k) {
s += p+':'+k[p]+'\n';
}
alert(s);
}

in Firefox and IE6 and both browsers work fine. What version of IE are
you using? As long as you use square-bracket and not dot notation,
javascript should theoretically be able to handle any arbitrary string
as a hash identifier.

Plus, it looks like your hash table data structure can be reversed into
a array:

rankings = ['AAA', 'AA+', 'AA', 'AA-', 'A+' /* etc. */];

and your indices would come out the same as the stored values in your
current hashtable.

-David

Nov 24 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.