gpt4 book ai didi

javascript - 为数组中的每个对象添加属性

转载 作者:行者123 更新时间:2023-12-02 17:11:22 27 4
gpt4 key购买 nike

我正在制作一个应用程序,要求用户单击某个位置,然后通过 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)
}

最佳答案

你看过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";
});

}

关于javascript - 为数组中的每个对象添加属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24787435/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com