我有一個多維陣列,我用兩個 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
}
]
]
uj5u.com熱心網友回復:
我已經查看了您的代碼,對我而言,我已經將父陣列中每個陣列的第一個元素與legCrowFliesDistance. 我發現,你想重新宣告變數lat1,并lat2在你的calcCrow功能。我將這些重命名為lat1radand 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
}
]
]
uj5u.com熱心網友回復:
經過大量的故障排除后,我想出了一個有效的解決方案,但我不知道它為什么有效,只是它確實有效。我使用 JSON.stringify 將我的資料轉換為 JSON,然后使用 JSON.parse 決議它,然后在正常作業之前無法正常作業的函式。這是我更改的唯一代碼行。
原始代碼:
console.log(calcCrowFliesTripMiles(this.state.allCombinations))
新代碼:
console.log(calcCrowFliesTripMiles(JSON.parse(JSON.stringify(this.state.allCombinations))))
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/382277.html
標籤:javascript 数组 for循环
上一篇:無法切片陣列
下一篇:匹配列并洗掉Shell中的重復項
