使用JavaScript查找同一对象数组中存在的两个值

时间:2019-02-17 12:49:22

标签: javascript arrays

我有一个包含航班详细信息的数组,例如机票的出发地,出发地和费用。我正在尝试显示特定出发地的航班详细信息。 例如:如果用户选择从bangaluru和从delhi中选择,那么我应该显示相应的详细信息。我使用set来打印唯一的值。是否有任何方法可以检查来自和属于同一个对象,那么它应该显示对象的详细信息。

我尝试了以下操作,但是它只是检查整个数组。

 if((flightDetails.includes(orginVal)) && (flightDetails.includes(destinationVal))){
            alert("hello");
         }

Js代码:

       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>';
}

3 个答案:

答案 0 :(得分:1)

您可以使用filter,它将返回具有匹配项的数组。如果只期望一个,当然可以从其中弹出该元素:

var 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"}]}];
     
var originVal = "Delhi(DEL)";
var destinationVal = "Bangaluru(BLR)"
var matches = flightDetails.filter(detail => detail.from === originVal && detail.to === destinationVal);
console.log(matches);

由于您现有的代码已经收集了用户进行选择的确切起点和目的地,因此我认为使用includes没什么用。寻找完全匹配会更合适。

filter可能会返回多个结果,因此您应该从该结果列表中为用户提供进一步的选择可能性。

答案 1 :(得分:1)

您要比较的是js对象,而不是from-to字符串。

使用功能find并检查每个对象的属性from-to。这种方法会找到第一个匹配项,而不是其余匹配项,在这种情况下,您可以改用功能filter

我假设最终用户将写一个像originVal="Bangal"destinationVal="Del"

这样的字符串

let 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"  }]}],
    originVal = "Bangaluru",
    destinationVal = "Delhi",
    found = flightDetails.find(({from, to}) => from.includes(originVal) && to.includes(destinationVal));

if (found) console.log(found.detail);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 2 :(得分:0)

您可以进行输入,然后根据from和to属性找到对象

let flightDetails = [{"airline":"B-201","from":"Bangaluru(BLR)","to":"Delhi(DEL)","detail":[{"date":"2019-12-30","price":"3900","departTime":"12:00PM","arriveTime":"14:00PM","seats":"10"},{"date":"2019-12-31","price":"3000","departTime":"17:30PM","arriveTime":"19:30PM","seats":"3"},{"date":"2019-06-01","price":"2100","departTime":"09:00AM","arriveTime":"11:00AM","seats":"7"}]},{"airline":"B-202","from":"Delhi(DEL)","to":"Bangaluru(BLR)","detail":[{"date":"2019-12-30","price":"3000","departTime":"12:00PM","arriveTime":"14:00PM","seats":"10"},{"date":"2019-12-31","price":"3000","departTime":"17:30PM","arriveTime":"19:30PM","seats":"3"},{"date":"2019-06-01","price":"2100","departTime":"09:00AM","arriveTime":"11:00AM","seats":"7"}]}]

function values(){
  let fromIn = document.getElementById('from').value
  let toIn = document.getElementById('to').value
  let details = flightDetails.find(( {from,to} ) => from === fromIn && to === toIn)
  console.log(details)
}
<input id='from' placeholder='From'></input>
<input id='to' placeHolder='To'></input>
<button onClick=values()>Give me details</button>