Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

319
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda