通过javascript中的动态变量访问对象

时间:2017-09-23 07:36:14

标签: javascript angularjs

我想访问动态变量中的对象

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

我想让变量动态我尝试了类似的东西

Resulttest

但它没有用,请帮我解决这个问题

2 个答案:

答案 0 :(得分: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 };
	 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>

答案 1 :(得分:1)

两件事:

1)你缺少使用positiondifferentplaces而不是$ scope.positiondifferentplaces for for循环。

2)您正在每个循环中重新初始化directionCoordinates。相反,您应该将for循环外的directionCoordinates定义为数组,然后在for循环中将对象推入其中

以下是完整的代码:

  $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
          }; 

}