【问题标题】:How to remove quotes from array如何从数组中删除引号
【发布时间】:2016-02-04 13:34:51
【问题描述】:

我的原生应用尝试从 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}]

【问题讨论】:

    标签: angularjs json angular-leaflet-directive


    【解决方案1】:

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

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

    【讨论】:

    • 天啊!谢谢!几个小时我尝试 .replace, Number for string ...)))
    【解决方案2】:

    使用JSON.parse()

    例如:

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

    【讨论】:

      猜你喜欢
      • 2021-06-26
      • 1970-01-01
      • 1970-01-01
      • 2017-04-05
      • 1970-01-01
      • 2021-11-29
      • 2019-06-04
      • 2020-02-29
      相关资源
      最近更新 更多