在javascript对象中查找匹配的值

时间:2018-04-15 10:10:10

标签: javascript

let flightInfo = {
userInputFlightNumber: 'KL856',
};

const xhr2 = new XMLHttpRequest;
const url2 = 'https://api.myjson.com/bins/11uvvr';


xhr2.onreadystatechange = function(){
  if (xhr2.readyState === XMLHttpRequest.DONE){
   let converTimetableToObject = JSON.parse(xhr2.response);
    console.log(converTimetableToObject);

          let resultArray = converTimetableToObject.response.flight.filter(function () {
              return (converTimetableToObject.response.flight.iata_number == flightInfo.userInputFlightNumber);
          });


console.log(resultArray);
console.log(flightInfo.userInputFlightNumber);
console.log(converTimetableToObject);


     }
   }
xhr2.open('GET', url2);
xhr2.send();

我的变量 converTimetableToObject 包含多个对象。例如:

response: [
{
flight: {
number: "8421",
iata_number: "AF8421",
icao_number: "AFR8421"
departure_time: 8:00,
},
flight: {
number: "856",
iata_number: "KL856",
icao_number: "KLM856"
departure_time: 10:00,
}
flight: {
number: "5925",
iata_number: "KE5925",
icao_number: "KAL5925"
departure_time: 12:00,
},
flight: {
number: "856",
iata_number: "KL856",
icao_number: "KLM856"
departure_time: 15:30,
}

属性 flightInfo.userInputFlightNumber 保存用户输入,该用户输入也是iata_number。如何过滤所有匹配的航班号?

我尝试了过滤方法,但它不断返回一个空数组。

 xhr2.onreadystatechange = function(){
 if (xhr2.readyState === XMLHttpRequest.DONE){
 let converTimetableToObject = JSON.parse(xhr2.response);
 let resultArray = converTimetableToObject.response.filter(function () 
 {return (converTimetableToObject.response.flight.iata_number == 
 flightInfo.userInputFlightNumber); });    }   }

我的目标是匹配iatacodes并显示匹配值的出发时间。

3 个答案:

答案 0 :(得分:0)

如果你看看filter是如何运作的,那就不是你所期望的了。在内部过滤器中,您应该有一个函数,并作为您为数组元素提供的函数的参数。所以这样的事情。您还可以通过将参数添加到回调和控制台来查看它是如何工作的。记录参数以查看当前元素是什么。

arr.filter(e => return e.flight.iata_number === flightInfo.userInputFlightNumber)

答案 1 :(得分:0)

我认为你的数组不正确,因为它有重复的属性名称'flight' 它应该像

 var obj='{"response":{"flight": [{ "number": "8421", "iata_number": "AF8421", "icao_number": "AFR8421", "departure_time": "8: 00" }, { "number": "856", "iata_number": "KL856", "icao_number": "KLM856", "departure_time": "10: 00" }, { "number": "5925", "iata_number": "KE5925", "icao_number": "KAL5925", "departure_time": "12: 00" }, { "number": "856", "iata_number": "KL856", "icao_number": "KLM856", "departure_time": "15: 30" }     ]   } }';

然后您可以将过滤器应用为

var arr=converTimetableToObject.response.flight;

var filtered=arr.filter(el=> el.iata_number===flightInfo.userInputFlightNumber);

答案 2 :(得分:0)

好吧,你的JSON完全无效。

您应该使用数组或对象打开JSON 必须使用双引号引用文件中的所有项目 重复的密钥也是非法的,除非在另一个对象或阵列下:所有的"航班" :{}键不起作用,但内部的各种键很好,因为它们通过不同的对象引用。 (所以你的converTimetableToObject.response.flight.iata_number匹配每个条目。)
逗号属于每个对象的末尾,除了单个对象或数组中的最后一个对象。 (在你的icao号码之后你没有一个,但你在出发时间之后做了。)

这就是你的JSON应该是这样的:

{
"response": {
    "flight": [{
            "number": "8421",
            "iata_number": "AF8421",
            "icao_number": "AFR8421",
            "departure_time": "8: 00"
        },
        {
            "number": "856",
            "iata_number": "KL856",
            "icao_number": "KLM856",
            "departure_time": "10: 00"
        },
        {
            "number": "5925",
            "iata_number": "KE5925",
            "icao_number": "KAL5925",
            "departure_time": "12: 00"
        },
        {
            "number": "856",
            "iata_number": "KL856",
            "icao_number": "KLM856",
            "departure_time": "15: 30"
        }
    ]
}
}

然后您必须通过for循环处理飞行阵列以检索所有iata_numbers。

这样的事情:

xhr2.onreadystatechange = function(){
  let resultArray = [];
  let flight = converTimetableToObject.response.flight;
  let lastFlight = flight.length;
  for (let i = 0; i < lastFlight; i++) {
    if (flight[i].iata_number == flightInfo.userInputFlightNumber) {
      resultArray.push(flight[i].iata_number) // push adds a new item to an existing array
    }
  }
  // do something with resultArray
}

编辑:我想你可以对filter做点什么,但这个功能对我来说是新的,所以请按照其他人的例子进行。