jquery - 如何遍历 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');
}
});
}
}
任何帮助将不胜感激。
解决方案
用于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'))
您的代码示例的其余部分不完整,因此我无法在代码中完全回答您的问题。
推荐阅读
- arrays - numpy:非结构化数组到结构化数组
- python - 如何将网络摄像头拍摄的图像保存到特定位置
- python - 使用 DTW 对齐视频序列
- json - 使用sql存储过程获取json字符串中的子节点?
- codeigniter - 在codeigniter中获取两个日期之间的小时和天差
- node.js - Firebase firestore 不适用于 Angular 10 和 SSR 或预渲染
- nativescript - 带有 Angular 的 FAB 菜单 Nativescript
- javascript - TS 错误:“事件”类型缺少“键盘事件”类型中的以下属性
- html - 引导导航栏汉堡菜单在 django 应用程序中不起作用
- mongodb - 过滤已过滤的 mongodb 集合