簡體   English   中英

向數組中的每個對象添加屬性

[英]Adding a property to each object in an array

我正在制作一個需要用戶單擊某個位置然后通過Google Maps定向到該位置的應用程序。這些位置是一組對象。 這就是位置文件的外觀。

$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()方法將其添加.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)
      }

您看過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";
     });

}

您可以在循環中在那里已經有angular.forEach($scope.SiteLocs, function(location) {...}只需在該函數中添加location.carrier = "sprint";

有時我使用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';
  });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM