473,320 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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
1 3400

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Brian van den Broek | last post by:
Hi all, I guess it is more of a maths question than a programming one, but it involves use of the decimal module, so here goes: As a self-directed learning exercise I've been working on a...
1
by: Les Juby | last post by:
Some time back we assembled an Explore-type menu system (tree of folders) for a client site which has grown to be a pivotal function of the site in that deep navigation into the South African...
8
by: Avin Patel | last post by:
Hi, Does the Double has the facilty to define -0. If it has, "==" or System.Math.Sign() doesn't able to differentiate between -0 & 0. i.e. Double x = -0.0d Double y = 0.0d if(value ==...
0
by: Emil Georgiev | last post by:
Hell I have a Web Custom Control project in ASP.NET. I'm using a subclassing technique to add functionality in HyperLink web server control. I want to create a property "BrowserWindow" of my...
1
by: qwerty | last post by:
My end goal is to have a script that takes user input from one pageA, submits to pageB which then displays the parameter values entered on PageA. My problem is that the values are being displayed...
4
by: tamnguyet | last post by:
I have n days and a date, I want a date plus(or minus) to n days.How can I do that in javascript
3
by: Mateo | last post by:
(As.Net 1.1 framework!) Hi! So, I have problem as described in subject. Here is source code:
2
by: pbd22 | last post by:
Hi. Can somebody tell me how to prevent a postback when I click on the little "plus" sign next to the treenode's root folder? It postbacks and I have to restart a video stream coming to the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.