Home > database >  how i can change code from var array to code from json?
how i can change code from var array to code from json?

Time:10-13

hi i need help to write a format code. i write javascript code to parse json and use the data, the json file contain latitude and longtitude data :

const api_url = 'json.php'
        async function getjsonlatest() {
            const response = await fetch(api_url);
            const data = await response.json();
            
            const latitude = data.map(loc => loc.latitude)
            const longitude = data.map(loc => loc.longtitude)
            
            var latlngs = [
                            [-7.3248841543504, 112.63356271278819],        
                            [-7.354508491243568, 112.62849870236622]       
                                                                    ];
            var polyline = L.polyline(latlngs, {color: 'green'}).addTo(mymap);
            

my question is how i can change :

var latlngs = [
                        [-7.3248841543504, 112.63356271278819],        
                        [-7.354508491243568, 112.62849870236622]       
                                                                ];

with data from :

const latitude = data.map(loc => loc.latitude)
const longitude = data.map(loc => loc.longtitude)

CodePudding user response:

const latlngs = data.map(loc => ([loc.latitude, loc.longitude])

You can return array from your map function building array of corteges.

  • Related