【发布时间】:2022-01-18 04:57:34
【问题描述】:
我有一个多维数组,我用两个 for 循环遍历它。如果满足某些条件,即 j 的值为 0,那么我想运行代码以将附加字段插入到数组中。如果 j 大于 0,我想运行一个函数,然后将此更新应用于数组。
我的问题是这个。循环似乎工作得很好,但它似乎在某个时候更新了数组的错误部分,我不确定为什么。我提供了一个测试数据集和我所指的代码。根据我在“calcCrowFliesTripMiles”函数中的代码,“legCrowFliesDistance”的 j=0 时的值应该等于“distanceFromKage”,但事实并非如此。我不确定这里发生了什么,但我似乎无法弄清楚。
function toRad (Value) {
return Value * Math.PI / 180;
}
function calcCrow (lat1, lon1, lat2, lon2) {
var R = 6371; // km
var dLat = toRad(lat2 - lat1);
var dLon = toRad(lon2 - lon1);
var lat1 = toRad(lat1);
var lat2 = toRad(lat2);
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c;
return d;
}
function calcCrowFliesTripMiles (combinations) {
var stopArray = [];
stopArray = [...combinations];
for (let i = 0; i < stopArray.length; i++) {
for (let j = 0; j < stopArray[i].length; j++) {
if (j === 0) {
stopArray[i][j].legCrowFliesDistance = stopArray[i][j].distanceFromKage;
} else {
stopArray[i][j].legCrowFliesDistance = calcCrow(stopArray[i][(j - 1)].attributes[0].ucmlLat, stopArray[i][(j - 1)].attributes[0].ucmlLng, stopArray[i][j].attributes[0].ucmlLat, stopArray[i][j].attributes[0].ucmlLng);
}
}
}
return stopArray;
}
var testArray = [
[{
'ShipLOC': 'SANCO',
'attributes': [{
'ucmlLat': '43.881431',
'ucmlLng': '-92.496931',
}],
'totalLocationProductLength': 184,
'distanceFromKage': 159.39214641507564,
}], [{
'ShipLOC': 'MALVESEQ',
'attributes': [{
'ucmlLat': '40.936476',
'ucmlLng': '-72.653116',
}],
'totalLocationProductLength': 96,
'distanceFromKage': 1691.1958136706187,
}], [{
'ShipLOC': 'MONTRA',
'attributes': [{
'ucmlLat': '42.286261',
'ucmlLng': '-71.598679',
}],
'totalLocationProductLength': 476,
'distanceFromKage': 1719.5409479837117,
}], [{
'ShipLOC': 'SANCO',
'attributes': [{
'ucmlLat': '43.881431',
'ucmlLng': '-92.496931',
}],
'totalLocationProductLength': 184,
'distanceFromKage': 159.39214641507564,
}, {
'ShipLOC': 'MALVESEQ',
'attributes': [{
'ucmlLat': '40.936476',
'ucmlLng': '-72.653116',
}],
'totalLocationProductLength': 96,
'distanceFromKage': 1691.1958136706187,
}], [{
'ShipLOC': 'SANCO',
'attributes': [{
'ucmlLat': '43.881431',
'ucmlLng': '-92.496931',
}],
'totalLocationProductLength': 184,
'distanceFromKage': 159.39214641507564,
}, {
'ShipLOC': 'MONTRA',
'attributes': [{
'ucmlLat': '42.286261',
'ucmlLng': '-71.598679',
}],
'totalLocationProductLength': 476,
'distanceFromKage': 1719.5409479837117,
}], [{
'ShipLOC': 'MALVESEQ',
'attributes': [{
'ucmlLat': '40.936476',
'ucmlLng': '-72.653116',
}],
'totalLocationProductLength': 96,
'distanceFromKage': 1691.1958136706187,
}, {
'ShipLOC': 'MONTRA',
'attributes': [{
'ucmlLat': '42.286261',
'ucmlLng': '-71.598679',
}],
'totalLocationProductLength': 476,
'distanceFromKage': 1719.5409479837117,
}], [{
'ShipLOC': 'SANCO',
'attributes': [{
'ucmlLat': '43.881431',
'ucmlLng': '-92.496931',
}],
'totalLocationProductLength': 184,
'distanceFromKage': 159.39214641507564,
}, {
'ShipLOC': 'MALVESEQ',
'attributes': [{
'ucmlLat': '40.936476',
'ucmlLng': '-72.653116',
}],
'totalLocationProductLength': 96,
'distanceFromKage': 1691.1958136706187,
}, {
'ShipLOC': 'MONTRA',
'attributes': [{
'ucmlLat': '42.286261',
'ucmlLng': '-71.598679',
}],
'totalLocationProductLength': 476,
'distanceFromKage': 1719.5409479837117,
}],
];
console.log(calcCrowFliesTripMiles(testArray));
.as-console-wrapper { min-height: 100%!important; top: 0; }
编辑: 这是另一个要测试的数据集,它稍微小一些,是我正在提取的实际数据的极简版本。当我使用 testArray 和使用我的实际数组时得到的结果是不同的。当我创建测试数组时,我从控制台复制实际数据,删除该函数中未使用的一些属性字段,然后将数据分配给数组。我不知道为什么两者的结果会不同,因为数据看起来完全一样,不包括附加属性字段。
数据:
[
[
{
"ShipLOC": "SANCO",
"attributes": [
{
"ucmlLat": "43.881431",
"ucmlLng": "-92.496931",
}
],
"totalLocationProductLength": 184,
"distanceFromKage": 159.39214641507564,
}
],
[
{
"ShipLOC": "MALVESEQ",
"attributes": [
{
"ucmlLat": "40.936476",
"ucmlLng": "-72.653116",
}
],
"totalLocationProductLength": 96,
"distanceFromKage": 1691.1958136706187,
}
],
[
{
"ShipLOC": "SANCO",
"attributes": [
{
"ucmlLat": "43.881431",
"ucmlLng": "-92.496931",
}
],
"totalLocationProductLength": 184,
"distanceFromKage": 159.39214641507564,
},
{
"ShipLOC": "MALVESEQ",
"attributes": [
{
"ucmlLat": "40.936476",
"ucmlLng": "-72.653116",
}
],
"totalLocationProductLength": 96,
"distanceFromKage": 1691.1958136706187,
}
]
]
When I run the code after assigning the above data to testArray, these are the results I get:
[
[
{
"ShipLOC": "SANCO",
"attributes": [
{
"ucmlLat": "43.881431",
"ucmlLng": "-92.496931",
}
],
"totalLocationProductLength": 184,
"distanceFromKage": 159.39214641507564,
}
],
[
{
"ShipLOC": "MALVESEQ",
"attributes": [
{
"ucmlLat": "40.936476",
"ucmlLng": "-72.653116",
}
],
"totalLocationProductLength": 96,
"distanceFromKage": 1691.1958136706187,
}
],
[
{
"ShipLOC": "SANCO",
"attributes": [
{
"ucmlLat": "43.881431",
"ucmlLng": "-92.496931",
}
],
"totalLocationProductLength": 184,
"distanceFromKage": 159.39214641507564,
},
{
"ShipLOC": "MALVESEQ",
"attributes": [
{
"ucmlLat": "40.936476",
"ucmlLng": "-72.653116",
}
],
"totalLocationProductLength": 96,
"distanceFromKage": 1691.1958136706187,
}
]
]
使用 testArray 时的结果:
[
[
{
"ShipLOC": "SANCO",
"attributes": [
{
"ucmlLat": "43.881431",
"ucmlLng": "-92.496931"
}
],
"totalLocationProductLength": 184,
"distanceFromKage": 159.39214641507564,
"legCrowFliesDistance": 159.39214641507564
}
],
[
{
"ShipLOC": "MALVESEQ",
"attributes": [
{
"ucmlLat": "40.936476",
"ucmlLng": "-72.653116"
}
],
"totalLocationProductLength": 96,
"distanceFromKage": 1691.1958136706187,
"legCrowFliesDistance": 1691.1958136706187
}
],
[
{
"ShipLOC": "SANCO",
"attributes": [
{
"ucmlLat": "43.881431",
"ucmlLng": "-92.496931"
}
],
"totalLocationProductLength": 184,
"distanceFromKage": 159.39214641507564,
"legCrowFliesDistance": 159.39214641507564
},
{
"ShipLOC": "MALVESEQ",
"attributes": [
{
"ucmlLat": "40.936476",
"ucmlLng": "-72.653116"
}
],
"totalLocationProductLength": 96,
"distanceFromKage": 1691.1958136706187,
"legCrowFliesDistance": 1657.5070148937111
}
]
]
使用实际数据时的结果(删除了大部分属性字段):
[
[
{
"ShipLOC": "SANCO",
"attributes": [
{
"ucmlLat": "43.881431",
"ucmlLng": "-92.496931",
}
],
"totalLocationProductLength": 184,
"distanceFromKage": 159.39214641507564,
"legCrowFliesDistance": 159.39214641507564
}
],
[
{
"ShipLOC": "MALVESEQ",
"attributes": [
{
"ucmlLat": "40.936476",
"ucmlLng": "-72.653116",
}
],
"totalLocationProductLength": 96,
"distanceFromKage": 1691.1958136706187,
"legCrowFliesDistance": 1657.5070148937111
}
],
[
{
"ShipLOC": "SANCO",
"attributes": [
{
"ucmlLat": "43.881431",
"ucmlLng": "-92.496931",
}
],
"totalLocationProductLength": 184,
"distanceFromKage": 159.39214641507564,
"legCrowFliesDistance": 159.39214641507564
},
{
"ShipLOC": "MALVESEQ",
"attributes": [
{
"ucmlLat": "40.936476",
"ucmlLng": "-72.653116",
}
],
"totalLocationProductLength": 96,
"distanceFromKage": 1691.1958136706187,
"legCrowFliesDistance": 1657.5070148937111
}
]
]
【问题讨论】:
-
我更新了我的帖子。我相信这是重现问题的极简主义。
标签: javascript arrays for-loop