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;

JavaScript

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.

JavaScript

When I manually type the variable in such as;

JavaScript

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.

JavaScript

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