Skip to content
Advertisement

Create array dynamically for javascript

I am trying to show markers on google map. And it is working fine if I use static array for marker’s lat and long.

But when I try to make dynamic using php array, then it is not working.

Here is the code.

JavaScript

I want following array in above code to be dynamic

JavaScript

For that I am using following code to make it dynamic

JavaScript

But it is not working. To test whether loop is working, I tried to print in console value (item.latitude) in loop, And values are being printed, Means loop is working fine.

But if I use loop inside js array then it is not working. Nor giving error through which I can identify some problem.

How I can make that array dynamic?

Update

JavaScript

This is json array, I am getting from api. And I converted that array in php array to make it use on my page.

Advertisement

Answer

The forEach does not return anything. Also you do not have any JSON in the client. It is already an object

You need a map – unless you simply create the array as it is supposed to look like on the server

Here I also convert the lat/long to a float

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