Javascript 如何检查对象是否在json中?

Javascript 如何检查对象是否在json中?,javascript,jquery,google-maps,autocomplete,Javascript,Jquery,Google Maps,Autocomplete,我正在使用google maps autocomplete,并在如下字段中检索每个对象: $("#usp-custom-21").val(arrAddress.filter(x => x.types.includes("route"))[0].long_name); $("#usp-custom-22").val(arrAddress.filter(x => x.types.includes("street_number"))[0].long_name); $("#usp-custo

我正在使用google maps autocomplete,并在如下字段中检索每个对象:

$("#usp-custom-21").val(arrAddress.filter(x => x.types.includes("route"))[0].long_name);
$("#usp-custom-22").val(arrAddress.filter(x => x.types.includes("street_number"))[0].long_name);
$("#usp-custom-23").val(arrAddress.filter(x => x.types.includes("locality"))[0].long_name);
$("#usp-custom-8").attr("value", arrAddress.filter(x => x.types.includes("country"))[0].long_name);
$("#usp-custom-25").val(arrAddress.filter(x => x.types.includes("postal_code"))[0].long_name);
但有时确实会出现这样的情况,我们没有这些字段中的一些或所有字段,我们会得到以下错误:

未捕获的TypeError:无法读取未定义的属性“long_name”

我尝试通过检查这些字段是否存在来执行条件设置,我尝试了检查未定义的字段或简单地检查是否存在:

if(arrAddress.filter(x => x.types.includes("route"))[0] != "undefined") {
   $("#usp-custom-21").val(arrAddress.filter(x => x.types.includes("route"))[0].long_name);
}
if(arrAddress.filter(x => x.types.includes("street_number"))[0].long_name){
   $("#usp-custom-22").val(arrAddress.filter(x => x.types.includes("street_number"))[0].long_name);
}
但我仍然得到:

未捕获的TypeError:无法读取未定义的属性“long_name”

它指的是if中的字符串long_名称

完整代码:

  autocomplete.addListener('place_changed', function() {
    var place = autocomplete.getPlace();
    if (!place.geometry) {
      return;
    }
    if (place.geometry.viewport) {
      map.fitBounds(place.geometry.viewport);
    } else {
      map.setCenter(place.geometry.location);
    }
    marker.setPosition(place.geometry.location);
    currentLatitude = place.geometry.location.lat();
    currentLongitude = place.geometry.location.lng();
    var arrAddress = place.address_components;
    var Newlat = map.getCenter().lat();
    var NewLong = map.getCenter().lng();
    $("#usp-custom-19").val(parseInt(Newlat));
    $("#usp-custom-20").val(parseInt(NewLong));
    if(arrAddress.filter(x => x.types.includes("route"))[0] != "undefined") {
      $("#usp-custom-21").val(arrAddress.filter(x => x.types.includes("route"))[0].long_name);
    }
    if(arrAddress.filter(x => x.types.includes("street_number"))[0].long_name){
      $("#usp-custom-22").val(arrAddress.filter(x => x.types.includes("street_number"))[0].long_name);
    }
    if(arrAddress.filter(x => x.types.includes("locality"))[0].long_name) {
      $("#usp-custom-23").val(arrAddress.filter(x => x.types.includes("locality"))[0].long_name);
    }
    if(arrAddress.filter(x => x.types.includes("country"))[0].long_name) {
      $("#usp-custom-8").val(arrAddress.filter(x => x.types.includes("country"))[0].long_name);
    }
    if(arrAddress.filter(x => x.types.includes("postal_code"))[0].long_name) {
      $("#usp-custom-25").val(arrAddress.filter(x => x.types.includes("postal_code"))[0].long_name);
    }
    if(place.formatted_address) {
      $("#usp-custom-60").val(place.formatted_address);
    }
    $("#usp-custom-90").val(Newlat+","+NewLong);
    console.log(place.formatted_address);
  });
试试这个:

你试过这个吗

if(arrAddress.filter(x => x.types.includes("route"))[0].long_name != "undefined") {
   $("#usp-custom-21").val(arrAddress.filter(x => x.types.includes("route"))[0].long_name);
}
您可能还需要检查NaN或null类型。当类型失败时,使用typeof确定类型:

console.log(typeof x.types.includes("route"))[0])


我不知道为什么是否决票,但它是通过检查未定义的,没有引号。如果有人能告诉我为什么这个答案不正确,我将不胜感激如果你已经解答了这个问题,请将它标记为已更正。我会尽快更正。是的,只有未定义的,没有引号的,按照其他答案工作。不好意思,我错过了这些引号还有console.logtypeof x.types.includeRoute[0]是否给出未捕获的引用错误:x未定义您是否将其放入闭包中?
console.log(typeof x.types.includes("route"))[0])
console.log(typeof x.types.includes("route"))[0].long_name)