Skip to main content
Inspiring
Apr 5, 2025 10:37 AM UTC
Answered

Creating a nested array in javascript for a google map

I'm trying to generate a map of tradespeople nearby to the site visitor. I've figured out most of it and am stuck on the final part mainly due to my weakness when it comes to arrays, structures etc.

 

Apparently, the api needs  an array that looks like:

 

 var locations = [
      ['Bondi Beach', -33.890542, 151.274856, 4],
      ['Coogee Beach', -33.923036, 151.259052, 5],
      ['Cronulla Beach', -34.028249, 151.157507, 3],
      ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
      ['Maroubra Beach', -33.950198, 151.259302, 1]
    ];

 

I'm adapting this for my application, i.e. not Australian beaches but company names.

 

function getNearbyTradies(oArg){
   var lat = oArg.lat || ""; 
   var lng = oArg.lng || "";
   var tradies = [];
   $.getJSON("cfcs/user.cfc?method=getNearbyUsers&returnformat=json&queryformat=column",{"lat":lat,"lng":lng},function(res,code){
      if(res.ROWCOUNT > 0){
         for (var i = 0; i<res.ROWCOUNT; i++) {
            var company_name = res.DATA.COMPANY_NAME[i];
            var lat = res.DATA.LAT[i];
            var lng = res.DATA.LNG[i];
            var trade_id = res.DATA.TRADIE_ID[i];
            var oTradie = "['" + company_name + "'," + lat + "," + lng + "," + trade_id + "]";
           //alert(oTradie); This gives ['Seal Plumbing',-36.9716195,174.8620086,121]
           tradies.push(oTradie);
         };
         //alert(tradies[0]); This gives ['Seal Plumbing',-36.9716195,174.8620086,121]
      };
   });
};

 

Is there a better way than manually constructing the inner array like this? Also, I don't think this method adds the comma at the end of each nested array.

 

How can I view what the entire structure looks like to see if it conforms with the Aussie beaches template? I can only alert each inner array.

 

paul_8809Correct answer
Inspiring
Apr 6, 2025 1:30 AM UTC

And finally, I got rid of the double quotes using the following

str = JSON.stringify(tradies, null, 4);
let cleanedString = str.replace(/"/g, '');
alert(cleanedString);

So I got the end resut, buf it feels like one big bodge exercise. Still keen to know if there is a cleaner way.

 

 

3 replies

paul_8809Author
Inspiring
Apr 6, 2025 12:48 AM UTC

Well, I've found out how to view the entire structure with an alert, if anyone is interested.

str = JSON.stringify(tradies, null, 4);
alert(str);

This produces:

[
    "['Pan Plumbing Ltd',-36.9716195,174.8620086,120]",
    "['Seal Plumbing',-36.9716195,174.8620086,121]"
]

The result is almost perfect apart from the quotes around each inner structure

 

Translate
paul_8809AuthorCorrect answer
Inspiring
Apr 6, 2025 1:30 AM UTC

And finally, I got rid of the double quotes using the following

str = JSON.stringify(tradies, null, 4);
let cleanedString = str.replace(/"/g, '');
alert(cleanedString);

So I got the end resut, buf it feels like one big bodge exercise. Still keen to know if there is a cleaner way.

 

 

Translate
BKBK
Community Expert
Community Expert
Apr 6, 2025 4:40 PM UTC

I am glad to hear that you found your answer. 

The only suggestion I can give is that you ask Javascript questions in a Javascript forum. You will then, in all likelihood, get quicker and better answers from there.

Translate