![](/img/trans.png)
[英]Adding property to each object of an array independently with AngularJS angular.extend
[英]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.