Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Creating a nested array in javascript for a google map

Explorer ,
Apr 05, 2025 Apr 05, 2025

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.

 

TOPICS
Advanced techniques
179
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Explorer , Apr 05, 2025 Apr 05, 2025

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
Explorer ,
Apr 05, 2025 Apr 05, 2025

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 05, 2025 Apr 05, 2025

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 06, 2025 Apr 06, 2025
LATEST

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources