首页 > 解决方案 > 如何遍历 Jquery 中的 JSON 对象以匹配字符串然后返回特定值?

问题描述

对 JQuery 来说非常新,并且在搜索 JSON 对象并返回特定结果时遇到问题。这是我拥有的对象:

"serviceProviderLocationList": [
        {
            "serviceProvider": {
                "value": "NBN"
            },
            "locationList": [
                {
                    "sourceSystemAddressId": null,
                    "serviceProvider": "NBN",
                    "locationId": "LOC000158748472",
                    "address": {
                        "subAddressNumber": "121",
                    },
                }
            ]
        },
        {
            "serviceProvider": {
                "value": "Telstra"
            },
            "locationList": [
                {
                    "sourceSystemAddressId": null,
                    "locationId": "266292692",
                    "address": {
                        "subAddressNumber": null,
                        },
                    },
                },
                {
                    "sourceSystemAddressId": null,
                    "locationId": "427275955",
                    "address": {
                        "subAddressNumber": "LIFT",
                    },
                },
                {
                    "sourceSystemAddressId": null,
                    "locationId": "267743661",
                    "address": {
                        "subAddressNumber": "100",
                    },
                },
                {
                    "sourceSystemAddressId": null,
                    "locationId": "267743660",
                    "address": {
                        "subAddressNumber": "99",
                        },
                    },
                }
            ]
        },
        {
            "serviceProvider": {
                "value": "AAPT"
            },
            "locationList": []
        }
    ]
}

我想要做的是搜索对象的部分:

serviceProviderLocationList.ServiceProvider.value == 'Telstra'

然后搜索相关的 LocationList,如果:

this.complexAddress.unit == locationList.address.subAddressNumber

返回正确的位置 ID。

例如,如果 this.complexAddress.unit == '100',那么它应该返回 Location ID == 267743661

或者 this.complexAddress.unit == 'null',那么它会返回 Location ID == 266292692

这是我目前用来获取 JSON 对象的代码:

GetTelstraServiceID: 函数 getTelstraServiceID( 事件 ) { event.preventDefault();

    var data = {
        'address': this.complexAddress,
        'action': 'get_location_id'
    },

    $.ajax({
        method: "POST",
        url: site_vars.ajaxurl,
        data: data
    })
    .done(function( response ) {



        var data = $.parseJSON( response ),
            locationID = data.serviceProviderLocationList[1].locationList[0].locationId;



        if ( typeof locationID !== 'undefined' ) {
            that.locationID = locationID;

        } else {
            alert('We couldn\'t find this address, sorry');
        }
    });
}

}

任何帮助将不胜感激。

标签: jqueryarrays

解决方案


用于Array.prototype.filter()查找您要查找的提供商和 locationID。

function getLocationId(data, provider, unit) {
  return data.filter(d => d.serviceProvider.value===provider)[0].locationList.filter(d => d.address.subAddressNumber === unit)[0].locationId;
}

const data = JSON.parse(`[{
    "serviceProvider": {
      "value": "NBN"
    },
    "locationList": [{
      "sourceSystemAddressId": null,
      "serviceProvider": "NBN",
      "locationId": "LOC000158748472",
      "address": {
        "subAddressNumber": "121"
      }
    }]
  },
  {
    "serviceProvider": {
      "value": "Telstra"
    },
    "locationList": [{
        "sourceSystemAddressId": null,
        "locationId": "266292692",
        "address": {
          "subAddressNumber": null
        }
      },
      {
        "sourceSystemAddressId": null,
        "locationId": "427275955",
        "address": {
          "subAddressNumber": "LIFT"
        }
      },
      {
        "sourceSystemAddressId": null,
        "locationId": "267743661",
        "address": {
          "subAddressNumber": "100"
        }
      },
      {
        "sourceSystemAddressId": null,
        "locationId": "267743660",
        "address": {
          "subAddressNumber": "99"
        }
      }
    ]
  },
  {
    "serviceProvider": {
      "value": "AAPT"
    },
    "locationList": []
  }
]`)

console.log(getLocationId(data, 'Telstra', null))
console.log(getLocationId(data, 'Telstra', '100'))

您的代码示例的其余部分不完整,因此我无法在代码中完全回答您的问题。


推荐阅读