0

I'm trying to use OpenStreetMap API. In the docs, we have an example Object :

var villes = {
            "Dijon": { "lat": 47.3215806, "lon": 5.0414701 },
            "Montbard": { "lat": 47.6241674, "lon": 4.3373194 }
        };

Now I would like to replace this static object by a dynamic object, assigned dynamically in a loop and each iteration is added to the object with Object.assign()

In result, the Object looks well formated but it doesn't work. The only difference I can see is in console. What is the difference between them ? I couldn't understand. (the first one looks empty but values are well stored. The second is working and it's the static one)

enter image description here


EDIT : code added below

Here is the code where the dynamic object is built :

var mesvilles = {};
        var ville_current = {};

        for (let i=0; i < cities.length; i++){
            $.getJSON('https://nominatim.openstreetmap.org/search.php?q='+cities[i]+'&country=France&format=json', function(data) {

                ville_current = {[cities[i]]: {"lat": parseFloat(data[0].lat), "lon": parseFloat(data[0].lon)}};
                mesvilles = Object.assign(mesvilles,ville_current);
            });
        }
        console.log(mesvilles);

Here is the original Object:

var villes = {
            "Dijon": { "lat": 47.3215806, "lon": 5.0414701 },
            "Montbard": { "lat": 47.6241674, "lon": 4.3373194 }
        };
        console.log(villes);
Ty Yt
  • 466
  • 2
  • 9
  • 25

0 Answers0