+ Start a Discussion
NM AdminNM Admin 

How to form a map in javascript dynamically.

Hello,

I have requirement that want to fetch data from controller to javascript and want to form a Map in javascript but I'm not able to form a map in javascript. Any way to solve this.
here is my current code which is not getting output:
var latLangAddMap = new Map();
var existingLat;
var existingLong;
var existingLatLng;
var existingAddress;


for (var i=0; i<result.length; i++) {
    console.log('result i'+ JSON.stringify(result[i]));
     var id = result[i].Id;
     var name = result[i].Name;
     existingLat = result[i].Location_Name__Latitude__s;
     existingLong = result[i].Location_Name__Longitude__s;
     existingAddress = result[i].Address__c;
     existingLatLng = existingLat + ',' + existingLong;
     //console.log('existingLatLng=>  '+existingLatLng);
     if(existingAddress != '' || existingAddress != null){
        latLangAddMap.set(existingLatLng,'');
     }else{
         latLangAddMap.set(existingLatLng,existingAddress);
      }
       console.log('latLangAddMap=> '+latLangAddMap);
    }

Please have a look and let me any other way to form a map in js.
You can also test the output on this link:

https://www.javascripture.com/Map


Welcome to your suggestions!
Thanks,
Nilesh
Raj VakatiRaj Vakati
Try this code
 
var latLangAddMap = {};
var existingLat;
var existingLong;
var existingLatLng;
var existingAddress;


for (var i=0; i<result.length; i++) {
    console.log('result i'+ JSON.stringify(result[i]));
     var id = result[i].Id;
     var name = result[i].Name;
     existingLat = result[i].Location_Name__Latitude__s;
     existingLong = result[i].Location_Name__Longitude__s;
     existingAddress = result[i].Address__c;
     existingLatLng = existingLat + ',' + existingLong;
     //console.log('existingLatLng=>  '+existingLatLng);
     if(existingAddress != '' || existingAddress != null){
        latLangAddMap.set(existingLatLng,'');
     }else{
         latLangAddMap.set(existingLatLng,existingAddress);
      }
       console.log('latLangAddMap=> '+latLangAddMap);
    }

 
NM AdminNM Admin
Thanks Raj,

I choosed another way to achieve this.

Thanks,
Nilesh