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

时间:2018-11-21 16:03:23

标签: javascript jquery google-maps autocomplete

我正在使用Google Maps自动完成功能,并且正在检索像这样的字段中的每个对象:

$("#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'

我正在尝试通过检查这些字段是否存在来进行条件检查,我尝试同时检查undefined或只是检查其中是否存在:

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'

它指的是string long_name内的if

完整代码:

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

2 个答案:

答案 0 :(得分:2)

尝试一下:

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] != undefined){
   $("#usp-custom-22").val(arrAddress.filter(x => x.types.includes("street_number"))[0].long_name);
}

答案 1 :(得分:0)

您尝试过吗?

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.log(typeof x.types.includes("route"))[0].long_name)