Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

258
Vistas
Setting an array element with the key 'length'

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?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda