0

我有两个使用 getJSON jquery 从服务器获取的以下结构的 JSON 数据。

countrylist = [
    { "Country": "United States", "Code": "us" }, 
    { "Country": "Belgium", "Code": "be" }, 
    { "Country": "Argentina", "Code": "ar" },
    .
    .
    .
]

citylist = [
    { "City": "Abernant", "ContryCode": "us", "CityId"=1 }, 
    { "City": "Academy Park", "ContryCode": "be", "CityId"=2}, 
    { "City": "Abernathy", "ContryCode": "ar","CityId"=3 },
    .
    .
    .
]

我需要在我的 div 中显示城市、国家我如何从 Citylist 对象中的国家列表中查找国家代码并放置其完整国家。所以我可以显示

Abernant, United States
Academy Park, Belgium
Abernathy, Argentina

到目前为止我有这个代码

  $.getJSON('../GetCountrylist', function (data) {
    var countryList =data;
  });


  $.getJSON('../GetCitylist', function (data) {
    //this is where i need to Join the data
    //for each data dispaydata= data.City ',' + Country
  });
4

4 回答 4

1

您可以countrylist使用countrylist.Code作为属性进行转换。使获得国家成为一项微不足道的任务。

var countries = {};
$.each(countrylist, function(i, country){
  countries[ country.Code ] = country.Country;
});

现在,您可以迭代citylist并从countries.

$.each(citylist, function(j, city){
  console.log(city.City + "," + countries[ city.ContryCode ]);
});

看这里。

于 2013-01-13T23:47:23.543 回答
0

您可以使用Alasql JavaScript 库来做到这一点。

这是一个主要的运营商:

var data = alasql('SELECT city.City, country.Country FROM ? AS city \
     JOIN ? AS country ON city.CountryCode = country.Code',[citylist, countrylist]);

在 jsFiddle试试这个示例

Alasql 可以直接下载 JSON 数据到 SELECT 语句中:

alasql("SELECT city.City, country.Country \
     FROM JSON('../GetCountrylist') AS city \
     JOIN JSON('../GetCitylist') AS country \
     ON city.CountryCode = country.Code",[], function(data){
    // use data
});
于 2014-12-19T12:23:06.427 回答
0

这是一个可能的解决方案$.extend

var countrylist = [{ "Country": "United States", "Code": "us" }, { "Country": "Belgium", "Code": "be" }, {"Country": "Argentina", "Code": "ar" }],
        citylist = [{ "City": "Abernant", "Code": "us", "CityId":1},{ "City": "Academy Park", "Code": "be", "CityId": 2},{ "City": "Abernathy", "Code": "ar","CityId":3 }],
        newArray = [];  

$.each(citylist, function(idx,val){
   var code = val.Code;
   $.each(countrylist,function(x,valu){
        if(valu.Code === code){
          newArray.push($.extend({},valu,val));
        } 
      });  
    });     

    console.log(newArray);

http://jsfiddle.net/3j5Aw/

于 2013-01-13T23:45:53.120 回答
0
var cityList = [];
var countryList = [];

// Assumption: You get countryList populated before calling this one
$.getJSON('../GetCitylist', function (data) {
 cityList = data;

 var outputString = '';
 for(var i in cityList) { // these are each objects ?
  // Assumption: You have no spaces in your property names
  outputString += cityList[i].City + ',';
  // You could also use cityList[i]['City'] if you expect spaces in your property
  // names - same below - choose the accessor method that's most appropriate to
  // your data
  for(var j in countryList) { // these are also objects ?
    if(countryList[j].Code === cityList[i].CountryCode) {
      outputString += countryList[j].Country;
    }
  }
  outputString += '<br />';
 }
 $('.selector').html(outputString);
});
于 2013-01-13T23:38:39.937 回答