如何从数组中删除引号

时间:2016-02-04 13:34:52

标签: angularjs json angular-leaflet-directive

我的原生应用尝试从JSON放置标记。 JSON数据示例:

{
"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"
    }]}

我的控制器:

        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);
        });

这将返回标记数组,如下所示:[{" layer":" realworld"," lat":" 53.11691211703813" ," lng":" 26.03631556034088"}]

但我需要从LAT和LNG坐标中删除引号:[{" layer":" realworld"," lat":53.11691211703813," LNG":26.03631556034088}]

2 个答案:

答案 0 :(得分:1)

您必须将字符串值转换为数字值。您可以通过在它们前面添加一个+来实现,如下所示:

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

答案 1 :(得分:0)

使用JSON.parse()

E.g。 :

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