this.routeHandler = function () {
let stations = ['KSFM', 'KPWM']
let coordinates = [];
let allCoords = [];
if (Array.isArray(stations) == true) {
for (let i = 0; i < fetchRoute().length; i++) {
for (let j = 0; j < database.length; j++) {
if (stations[i] === database[j].ident) {
coordinates = [(Number(database[j].coordinates[0])), (Number(database[j].coordinates[1]))];
allCoords.push(coordinates)
}
}
}
console.log(allCoords)
return;
} else { }
In my current implementation, I am able to retrieve the desired outcome - two sets of coordinates that match the input station array. However, what I truly desire is to have a dynamic array defined by this.route() instead of manually specifying "stations" in the code block. The issue arises from this route being part of a constructor and not directly accessible as an array.
The source of this array is related to airport(s) that are either singular or multiple additions, followed by appending the missing "K" letter prefix to incomplete airport identifiers. Despite consistently obtaining an array output, why does my this.routeCoord function fail to recognize it as such?
let database = [];
function apdata() {
const url = "https://pkgstore.datahub.io/core/airport-codes/airport-codes_json/data/9ca22195b4c64a562a0a8be8d133e700/airport-codes_json.json";
$.getJSON(url, function (data) {
database = data;
for (let i = 0; i < database.length; i++) {
database[i].coordinates = database[i].coordinates.split(',');
}
runData();
});
}
apdata();
let sampleArr = ['2020-08-08', 'N11682', 'KSFM', 'KSFM', 'KRKD KPSM', '', '', '', '', '', '', '3.0', '3.0', '0.0', '0.0', '0.0', '3.0', '161.70', '0', '0', '3', '3', '3', '0.0', '0.0', '0.00', '0.00', '0.00', '0.00', '0', '', '', '', '', '', '', '3.0', '0.0', '0.0', '0.0', '', '', '', '', '', '', '', '', 'false', 'false', 'false', 'Wayne'];
function runData() {
console.log(flyData.route())
}
let flyData = new Flight(sampleArr[0], sampleArr[1], sampleArr[2], sampleArr[3], sampleArr[4], sampleArr[11]);
function Flight(Date, AircraftID, From, To, Route, TotalTime) {
this.date = Date;
this.aircraft = AircraftID;
this.from = function () {
if (From.length < 4) {
return 'K' + From
}
else {
return From
}
}
this.to = function () {
if (To.length < 4) {
return 'K' + To
}
else {
return To;
}
};
this.route = function () {
let stationList = [];
if (Route.length > 4) {
Route = Route.split(' ');
for (let i = 0; i < Route.length; i++) {
if (Route[i].length < 4) {
Route[i] = 'K' + Route[i]
}
stationList.push(Route[i])
}
return stationList
} else if (Route.length < 4) {
return 'K' + Route;
} else {
return Route
}
};
let fetchRoute = this.route;
this.time = TotalTime;
this.toCoordinates = function () {
for (let j = 0; j < database.length; j++) {
if (this.to() === database[j].ident) {
let coordinates = [(Number(database[j].coordinates[0])), (Number(database[j].coordinates[1]))];
return coordinates;
}
}
};
this.fromCoordinates = function () {
for (let j = 0; j < database.length; j++) {
if (this.from() === database[j].ident) {
return [(Number(database[j].coordinates[0])), (Number(database[j].coordinates[1]))];
}
}
};
this.routeHandler = function () {
let coordinates = [];
let allCoords = [];
if (Array.isArray(this.route()) == true) {
for (let i = 0; i < fetchRoute().length; i++) {
for (let j = 0; j < database.length; j++) {
if (this.route()[i] === database[j].ident) {
coordinates = [(Number(database[j].coordinates[0])), (Number(database[j].coordinates[1]))];
allCoords.push(coordinates)
}
}
}
return allCoords;
} else {
for (let j = 0; j < database.length; j++) {
if (this.route() === database[j].ident) {
coordinates = [(Number(database[j].coordinates[0])), (Number(database[j].coordinates[1]))];
}
}
return coordinates;
}
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
What could be causing the discrepancy in my approach?