简体   繁体   English

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

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

I am making an app that requires the user to click on a location and then will be directed to the location via Google Maps.The locations are an array of objects. 我正在制作一个需要用户单击某个位置然后通过Google Maps定向到该位置的应用程序。这些位置是一组对象。 This is what the location files look like. 这就是位置文件的外观。

$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...

I want to add another element to every item in the array. 我想向数组中的每个项目添加另一个元素。 I tried adding it in through the .concat() method but that didn't seem to work. 我尝试通过.concat()方法将其添加.concat()但这似乎没有用。 If I wanted to add "carrier": "sprint", to each location how would I do that? 如果我想在每个位置添加"carrier": "sprint",我该怎么做?

I forgot to add this bit of code. 我忘了添加这段代码。 It splits and reorders the coordinates in the files. 它将拆分并重新排序文件中的坐标。

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)
      }

Have you looked at angular.forEach ? 您看过angular.forEach吗? https://docs.angularjs.org/api/ng/function/angular.forEach https://docs.angularjs.org/api/ng/function/angular.forEach

Here's the working JSFiddle - http://jsfiddle.net/Wqu68/3/ 这是工作中的JSFiddle- http://jsfiddle.net/Wqu68/3/

Relevant code: 相关代码:

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";

Sometimes I use lodash or angular. 有时我使用lodash或angular。 They all similar. 他们都相似。

**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){ 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