Skip to content
Advertisement

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}}

Advertisement

Answer

The solution came in how I received the data from PHP to start with. When I changed the definition to; const callOBJb = <?php echo "[$callOBJb]" ?> to include the square brackets and then used; for (let val of callOBJb) {...etc all of the variables resolved properly.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement