Search code examples
angularjsjsonangular-leaflet-directive

How to remove quotes from array


My native app tries to place markers from JSON. Example of JSON data:

{
"status": "ok",
"count": 15,
"count_total": 600,
"pages": 40,

"posts": [
    {
        "ID": "2290",
        "title": "Title",
        "tag": "Tag",
        "lat": "53.11691211703813",
        "lng": "26.03631556034088",
        "thumb": "getImage-24-100x100.jpg",
        "fullimg": "getImage-24.jpg",
        "imgs": [
            {
                "imgurl": "getImage-24-300x200.jpg"
            }
        ],
        "place": "Place",
        "type": "Photo",
        "period": "Period",
        "year": "1985",
        "url": "URL",
        "author": "Author"
    }]}

My controller:

        var addressPointsToMarkers = function(points) {
          return points.map(function(ap) {
            return {
              layer: 'realworld',
                lat: ap.lat,
                lng: ap.lng
            };
          });
        };
        $http.get("sample.json").success(function(data) {
            $scope.markers = addressPointsToMarkers(data.posts);
        });

This returns array of markers, like this: [{"layer":"realworld","lat":"53.11691211703813","lng":"26.03631556034088"}]

But I need to remove quotes from LAT and LNG coordinates: [{"layer":"realworld","lat":53.11691211703813,"lng":26.03631556034088}]


Solution

  • You have to convert the string values into number values. You can do that by adding a + in front of them, like this:

    return points.map(function(ap) {
      return {
        layer: 'realworld',
        lat: +ap.lat,
        lng: +ap.lng
      };
    });