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

328
Vistas
Javascript 'for of' loop not properly resolving to the value

I have a simple array that I'm trying to iterate over but I'm apparently not understanding the 'for of' JavaScript loop. The following code returns exactly has it should;

const callOBJb = ['KD0NBH0BJ','W0DLKOBJ','WA0TJTOBJ'];
        for (let i of callOBJb) {  
            console.log(i);
        }

return: KD0NBHOBJ W0DLKOBJ WA0TJTOBJ

But the following code errors out with; "TypeError: i.getCenter is not a function. (In 'i.getCenter()', 'i.getCenter' is undefined)" because the variable 'i' does not resolve to one of the above.

for (let i of callOBJb) {
       var Omiddle = i.getCenter(); 
    }

When I manually type the variable in such as;

var Middle = W0DLKOBJ.getCenter();

It works just fine. What am I not understanding about how this should work? I don't think I can use the ForEach here at least I'm not having any more luck than the for...of.

I was asked what the resolved variable for W0DLKOBJ might look like.

alert(JSON.stringify(KD0NBHOBJ));
 {"_southWest":{"lat":39.204385,"lng":-94.60714},"_northEast":{"lat":39.20646,"lng":-94.60481}}
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

This works:

var Middle = W0DLKOBJ.getCenter();

because this:

W0DLKOBJ

is different from this:

'W0DLKOBJ'

If the array should contain the variable values and not string literals, don't use quotes:

const callOBJb = [KD0NBH0BJ, W0DLKOBJ, WA0TJTOBJ];
about 4 years ago · Juan Pablo Isaza Denunciar

0

The callOBJb variable is an Array of strings, and when you use for..of statement to perform a loop iteration, for each item in your iterable object you will get item at deposition corresponding to the time to loop bloc code is execute.

In you case as all items in the callOBJb array is simple string, javascript String.prototype Object doesn't define a function named getCenter that is the reason why you are getting

"TypeError: i.getCenter is not a function. (In 'i.getCenter()', 'i.getCenter' is undefined)

let callOBJb = ['KD0NBH0BJ','W0DLKOBJ','WA0TJTOBJ'];

for(let item of callOBJb){
  console.log(typeof item, item);
}

As you can see on each iteration the typeof item is always string.

about 4 years ago · Juan Pablo Isaza Denunciar

0

I went back to the php and redesigned the output to make conversation to Javascript easier. By doing that I was able to iterate through the values using "for (let val of callOBJb)" successfully.

about 4 years ago · Juan Pablo Isaza Denunciar
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