简体   繁体   English

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

[英]Finding two values existing in same object array using javascript

I have an array with flight detail like from, to and cost of the tickets.我有一个包含航班详细信息的数组,例如机票的出发地、目的地和费用。 I'm trying to show the flight details for particular from-to.我正在尝试显示特定从到的航班详细信息。 For example: if the user selects from as bangaluru and to as delhi then I should show the corresponding detail.I used set to have the print the unique values.Is there any way to check if from and to belongs to same object then it should show details of the object.例如:如果用户选择 from as bangaluru 和 to as delhi 那么我应该显示相应的详细信息。我使用设置来打印唯一值。有没有办法检查 from 和 to 是否属于同一个对象,那么它应该显示对象的详细信息。

I tried the below, but it just checks the overall array.我尝试了下面的方法,但它只是检查整个数组。

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

Js code: 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>';
}

You can use filter , which will return an array with matches.您可以使用filter ,它将返回一个匹配的数组。 If you only expect one, you can of course pop that element out of it:如果您只期望一个,您当然可以从中弹出该元素:

 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);

As your existing code already collects the exact origins and destinations, from which the user makes a selection, I don't think it is useful to use includes .由于您现有的代码已经收集了用户从中进行选择的确切起点和目的地,因此我认为使用includes没有用。 It will be more appropriate to look for exact matches.寻找精确匹配会更合适。

filter will possibly return multiple results, so you should probably give the user a further selection possibility from that result list. filter可能会返回多个结果,因此您可能应该从该结果列表中为用户提供进一步的选择可能性。

You're comparing a js object rather than the from-to strings.您正在比较 js 对象而不是from-to字符串。

Use the function find and check for each object the attributes from-to .使用函数find并检查每个对象的属性from-to This approach finds the first match and not the remaining matches, in that case, you can use the function filter instead.这种方法会找到第一个匹配项而不是剩余的匹配项,在这种情况下,您可以改用函数filter

I'm assuming the final user will write a string like originVal="Bangal" and destinationVal="Del"我假设最终用户会写一个字符串,如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; }

You can take input and than find the object based on from and to property您可以接受输入,然后根据 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM