我有一个多维数组,我正在用两个for循环遍历它。如果满足某些条件,即j的值为0,那么我想运行代码在数组中插入一个额外的字段。如果j高于0,我想运行一个函数,然后将此更新应用于数组。
我的问题是这个。循环似乎很有效,但它似乎在某个时候更新了数组的错误部分,我不确定为什么。我已经提供了一个测试数据集和我所指的代码;legCrowliesDistance;应等于";距离Kage";基于我在";calcCrowFliesTripMiles";函数,但它不是。我不知道这里发生了什么事,但我似乎搞不清楚。
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
}
]
]
我已经查看了您的代码,对我来说,我已经获得了父数组中每个数组的第一个元素,该元素的值与legCrowFliesDistance
相同。我发现您试图在calcCrow
函数中重新声明变量lat1
和lat2
。我将它们重命名为lat1rad
和lat2rad
,它似乎像您描述的那样工作。我不确定这是否是你想要的修复程序,但我没有其他输出可以比较。
这是代码:
let 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
}
],
[
{
"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));
function calcCrowFliesTripMiles (combinations) {
let 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;
}
function calcCrow(lat1, lon1, lat2, lon2) {
const R = 6371; // km
let dLat = toRad(lat2-lat1);
let dLon = toRad(lon2-lon1);
let lat1rad = toRad(lat1);
let lat2rad = toRad(lat2);
let a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1rad) * Math.cos(lat2rad);
let c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
let d = R * c;
return d;
}
// Converts numeric degrees to radians
function toRad(Value) {
return Value * Math.PI / 180;
}
希望这能有所帮助!
编辑:
这就是我在控制台中得到的:
[
[
{
"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":"MONTRA",
"attributes":[
{
"ucmlLat":"42.286261",
"ucmlLng":"-71.598679"
}
],
"totalLocationProductLength":476,
"distanceFromKage":1719.5409479837117,
"legCrowFliesDistance":1719.5409479837117
}
],
[
{
"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":"MONTRA",
"attributes":[
{
"ucmlLat":"42.286261",
"ucmlLng":"-71.598679"
}
],
"totalLocationProductLength":476,
"distanceFromKage":1719.5409479837117,
"legCrowFliesDistance":1701.836066634145
}
],
[
{
"ShipLOC":"MALVESEQ",
"attributes":[
{
"ucmlLat":"40.936476",
"ucmlLng":"-72.653116"
}
],
"totalLocationProductLength":96,
"distanceFromKage":1691.1958136706187,
"legCrowFliesDistance":1691.1958136706187
},
{
"ShipLOC":"MONTRA",
"attributes":[
{
"ucmlLat":"42.286261",
"ucmlLng":"-71.598679"
}
],
"totalLocationProductLength":476,
"distanceFromKage":1719.5409479837117,
"legCrowFliesDistance":173.81078287083193
}
],
[
{
"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":"MONTRA",
"attributes":[
{
"ucmlLat":"42.286261",
"ucmlLng":"-71.598679"
}
],
"totalLocationProductLength":476,
"distanceFromKage":1719.5409479837117,
"legCrowFliesDistance":173.81078287083193
}
],
[
{
"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":"MONTRA",
"attributes":[
{
"ucmlLat":"42.286261",
"ucmlLng":"-71.598679"
}
],
"totalLocationProductLength":476,
"distanceFromKage":1719.5409479837117,
"legCrowFliesDistance":173.81078287083193
}
]
]
经过大量的故障排除,我提出了一个有效的解决方案,但我不知道它为什么有效,只是它确实有效。我用JSON.stringify将数据转换为JSON,然后用JSON.parse解析它,然后用以前不工作的函数正确工作。这是我更改的唯一一行代码。
原始代码:
console.log(calcCrowFliesTripMiles(this.state.allCombinations))
新代码:
console.log(calcCrowFliesTripMiles(JSON.parse(JSON.stringify(this.state.allCombinations))))