Search code examples
javascriptjsonstringesriarcgis-js-api

concatenate string from JSON


I would like to pull the longitudes and latitudes from an ESRI geometry and concatenate them in to a long string (to be used in an API call).

I am struggling with how to accomplish this

The ESRI documentation for geometry (geometry specs) shows the structure of the object but my API call needs the latitude/longitudes in the following format:

long1,lat1,long2,lat2,long3,lat3 ...  long1, lat1

All I have to do is process the long/lats a little bit. Making a very simple example from the ESRI documentation

        MyTest = {
                    "rings": [
                        [
                            [-97.06138, 32.837],
                            [-97.06133, 32.836],
                            [-97.06124, 32.834],
                            [-97.06127, 32.832],
                            [-97.06138, 32.837]
                        ]
                    ],
                    "spatialReference": {
                        "wkid": 4326
                    }
                };

        alert(JSON.stringify(MyTest.rings[0]));

Will give me the rings (the Longitudes/Latitudes) (notice the first long/lat is repeated as the last long/lat)

I cannot seem to figure out how to strip off the [ and ] to create a string with just the longitudes and latitudes. For instance:

myTest2 = MyTest.rings[0];  // get the longitudes and latitudes
myTest3 = JSON.stringify(myTest2);
myTest4 = myTest3.replace("[","");

alert(JSON.stringify(myTest2));

alert(JSON.stringify(myTest4));

The replace will strip off one of the brackets but I cannot get it to do a global replace like this post stack javascript replace because my programming environment is all within ColdFusion and I need the quotes around the pattern.

Can someone point out my error please ? Thanks !


Solution

  • DEMO: http://jsfiddle.net/gunderjt/B5wsK/2/

    MyTest.rings[0].join()
    

    and join with no parameters delineates with commas automatically