删除数组的部分

时间:2014-07-11 17:26:44

标签: javascript html arrays angularjs google-maps

我正在创建一个应用程序来查找/导航到某些位置。我有这个代码,我试图删除' 0'部分位于坐标的末尾,以使其与Google地图兼容。我会通过手动完成它,但我有几千个位置,我必须处理,所以这是不切实际的。我试图使用.splice()方法无济于事。

$scope.SiteLocs = [{
        "name": "502 Nelson St, Greenville, MS 38701",
        "visibility": "0",
        "description": "502 Nelson St, Greenville, MS 38701",
        "styleUrl": "#waypoint",
        "Point": {
          "coordinates": "-91.05636,33.415485,0"
        }

      }, {
        "name": "242 Blackhawk Trace, Galena, IL 61036",
        "visibility": "0",
        "description": "242 Blackhawk Trace, Galena, IL 61036",
        "styleUrl": "#waypoint",
        "Point": {
          "coordinates": "-90.319778,42.390862,0"
        }
      }, {
        "name": "3747 Ocean Dr, Vero Beach, FL 32963",
        "visibility": "0",
        "description": "3747 Ocean Dr, Vero Beach, FL 32963",
        "styleUrl": "#waypoint",
        "Point": {
          "coordinates": "-80.358248,27.659094,0"
        }

2 个答案:

答案 0 :(得分:1)

我对Angular不熟悉(使用普通的JS进行调整,相应调整)但不应该这样做吗?

SiteLocs.forEach(function(location){
  location.Point.coordinates = location.Point.coordinates.slice(0,-2);
});

答案 1 :(得分:0)

您可以添加循环并检查最后两个字符是否为",0"如果是,请删除它们。  http://jsbin.com/yodeg/2/edit

$scope.SiteLocs = [
   {
  "name": "502 Nelson St, Greenville, MS 38701",
  "visibility": "0",
  "description": "502 Nelson St, Greenville, MS 38701",
  "styleUrl": "#waypoint",
  "Point": {
    "coordinates": "-91.05636,33.415485,0"
  }

}
   , 
   {
  "name": "242 Blackhawk Trace, Galena, IL 61036",
  "visibility": "0",
  "description": "242 Blackhawk Trace, Galena, IL 61036",
  "styleUrl": "#waypoint",
  "Point": {
    "coordinates": "-90.319778,42.390862,0"
  }
}
   , 
   {
  "name": "3747 Ocean Dr, Vero Beach, FL 32963",
  "visibility": "0",
  "description": "3747 Ocean Dr, Vero Beach, FL 32963",
  "styleUrl": "#waypoint",
  "Point": {
    "coordinates": "-80.358248,27.659094,0"
  }
   }];

 angular.forEach($scope.SiteLocs, function(location){



    var clength = location.Point.coordinates.length;

   // if last 2 characters in string are ",0"
    if (location.Point.coordinates.substring(clength-2,clength)===",0")
    {
      location.Point.coordinates = location.Point.coordinates.substring(0,clength-2);

    }

  });
相关问题