My challenge involves handling an array containing flight details such as origin, destination, and ticket cost. I am attempting to display the specific flight details for a given origin-destination pair. For instance: if the user chooses Bangalore as the origin and Delhi as the destination, I need to display the corresponding details. I utilized a set to obtain unique values. Is there a way to verify if the origin and destination belong to the same object so that it can display the object's details?
I experimented with the following code snippet, but it only checks the entire array.
if((flightDetails.includes(orginVal)) && (flightDetails.includes(destinationVal))){
alert("hello");
}
Javascript code:
flightDetails=[{
"airline": "B-201",
"from": "Bangaluru(BLR)",
"to": "Delhi(DEL)",
"detail": [{
"date": "2019-12-30",
"price": "3900",
"departTime": "12:00 PM",
"arriveTime": "14:00 PM",
"seats":"10"
}, {
"date": "2019-12-31",
"price": "3000",
"departTime": "17:30 PM",
"arriveTime": "19:30 PM",
"seats":"3"
}, {
"date": "2019-06-01",
"price": "2100",
"departTime": "09:00 AM",
"arriveTime": "11:00 AM",
"seats":"7"
}]
},{
"airline": "B-202",
"from": "Delhi(DEL)",
"to": "Bangaluru(BLR)",
"detail": [{
"date": "2019-12-30",
"price": "3000",
"departTime": "12:00 PM",
"arriveTime": "14:00 PM",
"seats":"10"
}, {
"date": "2019-12-31",
"price": "3000",
"departTime": "17:30 PM",
"arriveTime": "19:30 PM",
"seats":"3"
}, {
"date": "2019-06-01",
"price": "2100",
"departTime": "09:00 AM",
"arriveTime": "11:00 AM",
"seats":"7"
}]
}]
inputOrigin=document.getElementById('origin');
inputDesination= document.getElementById("desination");
originOptions=document.getElementById('originCountry');
destinationOptions= document.getElementById('desinationCountry');
var originCategories = new Set();
var destinationCategories = new Set();
flightDetails.forEach((o) => originCategories.add(o.from));
originCategories = [...originCategories];
flightDetails.forEach((o) => destinationCategories.add(o.to));
destinationCategories = [...destinationCategories];
for(i=0;i<originCategories.length;i++) {
originOptions.innerHTML+=' <option>'+originCategories[i]+'<option>';
}
for(i=0;i<destinationCategories.length;i++) {
destinationOptions.innerHTML+=' <option>'+destinationCategories[i]+'<option>';
}