向数组中的每个对象添加属性

时间:2014-07-16 17:49:06

标签: javascript arrays angularjs

我正在创建一个应用程序,要求用户点击某个位置,然后通过Google Map定位到该位置。这些位置是一个对象数组。这就是位置文件的样子。

$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" }
          }, ect...

我想为数组中的每个项添加另一个元素。 我尝试通过.concat()方法添加它,但这似乎不起作用。 如果我想将"carrier": "sprint",添加到每个位置,我该怎么做?

我忘了添加这段代码。它拆分并重新排序文件中的坐标。

angular.forEach($scope.SiteLocs, function(location) {
      var clength = location.Point.coordinates.length;
      if (location.Point.coordinates.substring(clength - 2, clength) === ",0") {
        location.Point.coordinates = location.Point.coordinates.substring(0, clength - 2).split(",");
        Lat = location.Point.coordinates[0]
        Lon = location.Point.coordinates[1]
        Com = ","
        location.Point.coordinates = Lon.concat(Com,Lat)
      }

3 个答案:

答案 0 :(得分:2)

你看过angular.forEach了吗? https://docs.angularjs.org/api/ng/function/angular.forEach

这是有效的JSFiddle - http://jsfiddle.net/Wqu68/3/

相关代码:

function appCtrl($scope, $http){

    $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(place) {
          place.carrier = "Sprint";
     });

}

答案 1 :(得分:0)

你可以在已经拥有angular.forEach($scope.SiteLocs, function(location) {...}的循环中直接在该函数中添加location.carrier = "sprint";

答案 2 :(得分:0)

有时我会使用lodash或angular。他们都很相似。

**Solution** using Lodash.js

//using forEach to loop each array
_.forEach($scope.SiteLocs, function(object){

***//adding carrier as property to each object in the array and giving value 'sprint'
// an identifier/variable can take the place of that value***

    object.carrier = 'sprint';
  });

**Solution** using angular.js
//using forEach to loop each array

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

***//adding carrier as property to each object in the array and giving value 'sprint'
// an identifier/variable can take the place of that value***

    object.carrier = 'sprint';
  });