0

I am building an array of spellings, and for rapid look-up I want to have the property of each element as the spelling, thus:

var spellings=[];
var spelling="fred";
spellings[spelling]="page 1";

This allows me, for any spelling, to retrieve information about it by a simple look-up: thus spellings["fred"] will return the string "page 1". This all worked perfectly swimmingly, for many thousands of spellings, until one day when my program found the spelling "length". So it happily did:

var spelling="length";
spellings[spelling]="page 2";

Whereupon, all hell broke loose. I get the error "Uncaught RangeError: Invalid array length". Because, of course, every javascript array has a property "length", which is a number which returns the number of elements in the array. Thus, if my spellings array has five elements in it, spellings.length (or spellings["length"]) returns the number 5. My attempt to assign a string or some other object to spellings["length"] accordingly fails.

In my case, there is a fairly easy work-around, which costs me a little bit of information. Just capitalize the initial of "length", thus:

var spelling="Length";
spellings[spelling]="page 2";

This works, but is not very cool. Alternatively, I could rewrite the whole process so that spellings are not used as keys to the array. Does anyone have a better solution?

peter
  • 195
  • 1
  • 1
  • 10

0 Answers0