【问题标题】:How to check if object is in json?如何检查对象是否在json中?
【发布时间】:2018-11-21 16:03:22
【问题描述】:

我正在使用谷歌地图自动完成功能,我正在检索字段中的每个对象,如下所示:

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

但有时我们没有部分或全部这些字段确实会发生,我们会收到以下错误:

未捕获的类型错误:无法读取未定义的属性“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);
}

但我仍然得到:

未捕获的类型错误:无法读取未定义的属性“long_name”

它指的是if中的string long_name

完整代码:

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

【问题讨论】:

    标签: javascript jquery google-maps autocomplete


    【解决方案1】:

    试试这个:

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

    【讨论】:

    • 我不知道为什么投反对票,但它通过检查未定义而没有引号来工作。如果有人能告诉我为什么这个答案不正确,我将不胜感激
    • 如果您已经解决了这个问题,请将其标记为已更正
    • 我会尽快做的
    【解决方案2】:

    你试过了吗?

    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)
    

    【讨论】:

    • 是的,我有,只有未定义且不带引号的情况下才能按照其他答案工作
    • 真丢脸,错过了这些名言
    • 还有 console.log(typeof x.types.includes("route")[0]) 给出了 Uncaught ReferenceError: x is not defined
    • 你把它放在封口里面了吗?
    猜你喜欢
    • 1970-01-01
    • 2012-06-26
    • 2011-05-16
    • 2014-01-15
    • 1970-01-01
    • 1970-01-01
    • 2015-04-02
    • 1970-01-01
    相关资源
    最近更新 更多