Reputation: 155
I want to access objects in a my dynamic variable
$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
}]
I just want to access latitude and Longitude form this value the required variable is something like this
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
} ];
I want to make that variable dynamic i have tried something like this
for (var i = 0; i < positionindifferentplaces .length; ++i) {
var directionCoordinates = {
lat :$scope.positionindifferentplaces [i].Latitude,
lng :$scope.positionindifferentplaces [i].Longitude }; }
but it's not working please help me with this
Upvotes: 0
Views: 48
Reputation: 1074
Two things:
1) You are missing using positiondifferentplaces instead of $scope.positiondifferentplaces in for cycle.
2) You are re initializing directionCoordinates in every for cycle. Instead, you should define directionCoordinates outside the for cycle as an array, and then push the objects into it in the for cycle
Here is the complete code:
$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
};
}
Upvotes: 1
Reputation: 30739
You can use this code:
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>
Upvotes: 1