【问题标题】:Removing Portions of an Array删除数组的一部分
【发布时间】:2014-09-02 09:46:17
【问题描述】:

我正在制作一个应用程序来查找/导航到某些位置。我有这段代码,并试图删除坐标末尾的 ',0' 部分,以使其与谷歌地图兼容。我会手动完成,但我有几千个位置需要处理,所以这是不切实际的。我尝试使用 .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"
        }

【问题讨论】:

  • 顺便说一句,splice() 不是字符串的方法
  • 使用splice删除数组的一部分遇到什么问题?

标签: javascript html arrays angularjs google-maps


【解决方案1】:

我不熟悉 Angular(玩纯 JS,相应调整)但这不应该工作吗?

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

【讨论】:

    【解决方案2】:

    您可以添加循环并检查最后两个字符是否为“,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);
    
        }
    
      });
    

    【讨论】:

    • 在你的 jsbin 中,'
      {{SiteLocs|json}}
      ' 有什么作用?
    • 它有助于测试以 json 格式显示您的对象
    • 这很奇怪。我无法弄清楚为什么您的代码在 jsbin 中有效,但在我的 plunker 中无效。你觉得你可以来看看我吗? plnkr.co/edit/OZZRgiEcrLzreW3lrc5v?p=preview
    • 请参阅plnkr.co/edit/ZKgKfTWtW4inZDq7lFC5?p=preview angular.forEach ... 在控制器之外。顺便说一下,大量的位置:)
    • 一切都好吗?
    猜你喜欢
    • 1970-01-01
    • 2011-12-06
    • 2013-11-14
    • 2014-02-15
    • 2011-12-20
    • 2019-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多