JQuery Map 和 JSON - 获取子数组元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3729971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery Map and JSON - Getting Sub Array Elements
提问by Frank
I'm using JQuery to get city data from geonames. My problem is when I want to extract the value for sub-array items when using the JQuery map function.
我正在使用 JQuery 从 geonames 获取城市数据。我的问题是当我想在使用 JQuery 映射函数时提取子数组项的值。
In this example, if I want to get the Wikipedia link, how could I do this?
在这个例子中,如果我想获得维基百科链接,我该怎么做?
I tried: wikipedia: item.alternateNames.lang['link']
我试过:维基百科:item.alternateNames.lang['link']
but didn't expect it would really work. Does anyone know who I can extract the sub array items?
但没想到真的能用。有谁知道我可以提取子数组项吗?
Here are the bits of code:
以下是部分代码:
JSON RESULT
JSON 结果
"geonames": [{
"alternateNames": [
{
"name": "Rancho Cucamonga",
"lang": "en"
},
{
"name": "91739",
"lang": "post"
},
{
"name": "http://en.wikipedia.org/wiki/Rancho_Cucamonga%2C_California",
"lang": "link"
}
],
adminCode2": "071",
"countryName": "United States",
"adminCode1": "CA",
"fclName": "city, village,...",
"elevation": 368,
"score": 0.9999999403953552,
"countryCode": "US",
"lng": -117.5931084,
"adminName2": "San Bernardino County",
"adminName3": "",
"fcodeName": "populated place",
JQuery:
查询:
$("#city").autocomplete({
source: function(request, response) {
$.ajax({
url: "http://ws.geonames.org/searchJSON",
dataType: "jsonp",
data: {
featureClass: "P",
style: "full",
maxRows: 12,
name_startsWith: request.term
},
success: function(data) {
response($.map(data.geonames, function(item) {
return {
label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
value: item.name,
latitude: item.lat,
longitude: item.lng,
// problem here: how to get the value for the alternateNames.lang where lang = link
wikipedia: item.alternateNames.lang['link']
}
}))
}
})
},
回答by BBonifield
Try...
尝试...
wikipedia: item.alternateNames[2].name
But if you wanted to search them...
但是如果你想搜索它们......
var name;
$.each( item.alternateNames, function(){
if ( this.lang == 'link' ) {
name = this.name;
return false;
}
});
回答by Stephen Watkins
Your alternateNames
is an array of objects, so you would have to use an index to retrieve an object from the array. Once you have the object you can retrieve the "lang" field:
您alternateNames
是一个对象数组,因此您必须使用索引从数组中检索对象。拥有对象后,您可以检索“lang”字段:
item.alternateNames[2].lang
that should return you "link". This is assuming that item
is the proper parent object of alternateNames
.
那应该返回给你“链接”。这是假设item
是 的正确父对象alternateNames
。
回答by FreeVice
In same accident I find next solve:
在同样的事故中,我找到了下一个解决方案:
Instead calling sub elements lang
undo map
, you can assigment only parent element wikipedia: item.alternateNames
. And after, you can access to nested elements of them in select event of autocomplete.
而不是调用子元素lang
撤销map
,你可以只分配父元素wikipedia: item.alternateNames
。之后,您可以在自动完成的选择事件中访问它们的嵌套元素。
This is my example:
这是我的例子:
$("#Customer_Name ").autocomplete({
source: function (request, response) {
$.ajax({
url: '@Url.Action("LookupCustomers", "Customers")', type: "POST", dataType: "json",
data: { searchText: request.term, maxResults: 10 },
success: function (data) {
response($.map(data, function (item) {
//respose($.each(data, function (item
return {
label: item.Name,
value: item.Name,
id: item.Id,
phone: item.Phone,
addressStreet: item.AddressStreet,
addressBuilding: item.AddressBuilding,
addressHousing: item.AddressHousing,
addressFlat: item.AddressFlat,
metroStationId: item.MetroStationId,
metroStation: item.MetroStation //trouble was here
}
}))
}
})
},
select: function (event, ui) {
document.getElementById("Customer_Id").value = ui.item.id;
document.getElementById("Customer_Name").value = ui.item.value;
document.getElementById("Customer_Phone").value = ui.item.phone;
document.getElementById("Customer_AddressStreet").value = ui.item.addressStreet;
document.getElementById("Customer_AddressBuilding").value = ui.item.addressBuilding;
document.getElementById("Customer_AddressHousing").value = ui.item.addressHousing;
document.getElementById("Customer_AddressFlat").value = ui.item.addressFlat;
document.getElementById("Customer_MetroStationId").value = ui.item.metroStationId;
document.getElementById("Customer_MetroStation_Name").value = ui.item.metroStation.Name; //and, trouble was here
}
});