2017-09-23 89 views
0

我要访问在我的动态变量访问对象的JavaScript

$scope.positionindifferentplaces = [ { 
    "DeviceName" : "Device 1", 
    "DeviceID" : "10000005", 
    "Date" : "2017-09-22T03:35:38-05:00", 
    "Latitude" : 12.9716, 
    "Longitude" : 77.5946, 
    "Type" : "GPS", 
    "Speed(mph)" : 64, 
    "Speed(km/h)" : 103, 
    "Altitude(ft)" : 68, 
    "Altitude(m)" : 21, 
    "Accuracy" : 5 
}, { 
    "DeviceName" : "Device 2", 
    "DeviceID" : "10000005", 
    "Date" : "2017-09-22T03:35:38-05:00", 
    "Latitude" : 17.3850, 
    "Longitude" : 78.4867, 
    "Type" : "GPS", 
    "Speed(mph)" : 64, 
    "Speed(km/h)" : 103, 
    "Altitude(ft)" : 68, 
    "Altitude(m)" : 21, 
    "Accuracy" : 5 
}, { 
    "DeviceName" : "Device 3", 
    "DeviceID" : "10000005", 
    "Date" : "2017-09-22T03:35:38-05:00", 
    "Latitude" : 21.2514, 
    "Longitude" : 81.6296, 
    "Type" : "GPS", 
    "Speed(mph)" : 64, 
    "Speed(km/h)" : 103, 
    "Altitude(ft)" : 68, 
    "Altitude(m)" : 21, 
    "Accuracy" : 5 
}, { 
    "DeviceName" : "Device 4", 
    "DeviceID" : "10000005", 
    "Date" : "2017-09-22T03:35:38-05:00", 
    "Latitude" : 28.7041, 
    "Longitude" : 77.1025, 
    "Type" : "GPS", 
    "Speed(mph)" : 64, 
    "Speed(km/h)" : 103, 
    "Altitude(ft)" : 68, 
    "Altitude(m)" : 21, 
    "Accuracy" : 5 
}] 

我只是想获得纬度和经度形式此值 所需的变量对象是这样的

var directionCoordinates = [ { 
     lat : 12.9716, 
     lng : 77.5946 
    }, { 
     lat : 17.3850, 
     lng : 78.4867 
    }, { 
     lat : 21.2514, 
     lng : 81.6296 
    }, { 
     lat : 28.7041, 
     lng : 77.1025 
    } ]; 

我想使这个变量的动态我已经试过这样的事情

for (var i = 0; i < positionindifferentplaces .length; ++i) { 
      var directionCoordinates = { 
     lat :$scope.positionindifferentplaces [i].Latitude, 
     lng :$scope.positionindifferentplaces [i].Longitude }; } 

但它不工作,请帮助我这个

回答

1

两件事情:

1)您使用的positiondifferentplaces,而不是失踪for循环中$ scope.positiondifferentplaces。

2)您正在为每个周期重新初始化方向坐标。相反,你应该定义directionCoordinates之外的周期为一个数组,然后将物体推到它在对周期

下面是完整的代码:

$scope.positionindifferentplaces = [ { 
    "DeviceName" : "Device 1", 
    "DeviceID" : "10000005", 
    "Date" : "2017-09-22T03:35:38-05:00", 
    "Latitude" : 12.9716, 
    "Longitude" : 77.5946, 
    "Type" : "GPS", 
    "Speed(mph)" : 64, 
    "Speed(km/h)" : 103, 
    "Altitude(ft)" : 68, 
    "Altitude(m)" : 21, 
    "Accuracy" : 5 
}, { 
    "DeviceName" : "Device 2", 
    "DeviceID" : "10000005", 
    "Date" : "2017-09-22T03:35:38-05:00", 
    "Latitude" : 17.3850, 
    "Longitude" : 78.4867, 
    "Type" : "GPS", 
    "Speed(mph)" : 64, 
    "Speed(km/h)" : 103, 
    "Altitude(ft)" : 68, 
    "Altitude(m)" : 21, 
    "Accuracy" : 5 
}, { 
    "DeviceName" : "Device 3", 
    "DeviceID" : "10000005", 
    "Date" : "2017-09-22T03:35:38-05:00", 
    "Latitude" : 21.2514, 
    "Longitude" : 81.6296, 
    "Type" : "GPS", 
    "Speed(mph)" : 64, 
    "Speed(km/h)" : 103, 
    "Altitude(ft)" : 68, 
    "Altitude(m)" : 21, 
    "Accuracy" : 5 
}, { 
    "DeviceName" : "Device 4", 
    "DeviceID" : "10000005", 
    "Date" : "2017-09-22T03:35:38-05:00", 
    "Latitude" : 28.7041, 
    "Longitude" : 77.1025, 
    "Type" : "GPS", 
    "Speed(mph)" : 64, 
    "Speed(km/h)" : 103, 
    "Altitude(ft)" : 68, 
    "Altitude(m)" : 21, 
    "Accuracy" : 5 
}]; 

var directionCoordinates = []; 
for (var i = 0; i < $scope.positionindifferentplaces .length; ++i) { 
      directionCoordinates.push({ 
      lat :$scope.positionindifferentplaces [i].Latitude, 
      lng :$scope.positionindifferentplaces [i].Longitude 
      }; 

} 
1

您可以使用此代码:

angular.module('app',[]).controller('mainCtrl', function($scope){ 
 
    $scope.positionindifferentplaces = [{ 
 
    "DeviceName" : "Device 1", 
 
    "DeviceID" : "10000005", 
 
    "Date" : "2017-09-22T03:35:38-05:00", 
 
    "Latitude" : 12.9716, 
 
    "Longitude" : 77.5946, 
 
    "Type" : "GPS", 
 
    "Speed(mph)" : 64, 
 
    "Speed(km/h)" : 103, 
 
    "Altitude(ft)" : 68, 
 
    "Altitude(m)" : 21, 
 
    "Accuracy" : 5 
 
}, { 
 
    "DeviceName" : "Device 2", 
 
    "DeviceID" : "10000005", 
 
    "Date" : "2017-09-22T03:35:38-05:00", 
 
    "Latitude" : 17.3850, 
 
    "Longitude" : 78.4867, 
 
    "Type" : "GPS", 
 
    "Speed(mph)" : 64, 
 
    "Speed(km/h)" : 103, 
 
    "Altitude(ft)" : 68, 
 
    "Altitude(m)" : 21, 
 
    "Accuracy" : 5 
 
}, { 
 
    "DeviceName" : "Device 3", 
 
    "DeviceID" : "10000005", 
 
    "Date" : "2017-09-22T03:35:38-05:00", 
 
    "Latitude" : 21.2514, 
 
    "Longitude" : 81.6296, 
 
    "Type" : "GPS", 
 
    "Speed(mph)" : 64, 
 
    "Speed(km/h)" : 103, 
 
    "Altitude(ft)" : 68, 
 
    "Altitude(m)" : 21, 
 
    "Accuracy" : 5 
 
}, { 
 
    "DeviceName" : "Device 4", 
 
    "DeviceID" : "10000005", 
 
    "Date" : "2017-09-22T03:35:38-05:00", 
 
    "Latitude" : 28.7041, 
 
    "Longitude" : 77.1025, 
 
    "Type" : "GPS", 
 
    "Speed(mph)" : 64, 
 
    "Speed(km/h)" : 103, 
 
    "Altitude(ft)" : 68, 
 
    "Altitude(m)" : 21, 
 
    "Accuracy" : 5 
 
}]; 
 
var directionCoordinates = []; 
 
$scope.positionindifferentplaces.forEach(function(item){ 
 
    var obj = {lat:item.Latitude , lng:item.Longitude }; 
 
\t directionCoordinates.push(obj); 
 
}); 
 

 
console.log(directionCoordinates); 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app='app' ng-controller='mainCtrl'> 
 
</div>