gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 03:02:23 25 4
gpt4 key购买 nike

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

$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)您在 for 循环中缺少使用positionotherplaces 而不是 $scope.positionotherplaces。

2) 您正在每个 for 循环中重新初始化方向坐标。相反,您应该在 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
};

}

关于javascript - javascript中通过动态变量访问对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46377242/

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