Javascript 连接或查找两个JSON数据集

Javascript 连接或查找两个JSON数据集,javascript,jquery,asp.net-mvc,jquery-ui,Javascript,Jquery,Asp.net Mvc,Jquery Ui,我有两个JSON数据,其结构如下 我使用getJSON jquery从服务器获取的 countrylist = [ { "Country": "United States", "Code": "us" }, { "Country": "Belgium", "Code": "be" }, { "Country": "Argentina", "Code": "ar" }, . . . ] citylist = [ { "City": "A

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

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中显示City,Country如何从Citylist对象的countrylist中查找countrCode,并将其完整的国家/地区显示出来。 所以我可以展示

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
  });
var cityList=[];
var countryList=[];
//假设:在调用此项之前,您已填充countryList
$.getJSON('../GetCitylist',函数(数据){
城市列表=数据;
var outputString='';
对于(cityList中的var i){//这些是每个对象吗?
//假设:属性名称中没有空格
outputString+=cityList[i]。城市+',';
//如果您希望在您的财产中有空间,也可以使用cityList[i]['City']
//名称-如下所示-选择最适合的访问器方法
//你的数据
对于(countryList中的var j){//这些也是对象吗?
if(countryList[j].Code==cityList[i].CountryCode){
outputString+=countryList[j]。国家;
}
}
outputString+='
'; } $('.selector').html(输出字符串); });
这里有一个可能的解决方案,使用
$。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);

您可以使用
countrylist.code
作为属性来转换
countrylist
。让国家成为一项微不足道的任务

var countries = {};
$.each(countrylist, function(i, country){
  countries[ country.Code ] = country.Country;
});
现在,您可以迭代
城市列表
,从
国家
获取国家

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

您可以使用JavaScript库来完成

这是一个主要操作员:

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

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

你知道你有很多缺少的qoutes(
'
)对吧?
$.getJSON('../GetCountrylist,函数(数据){
你在
'../GetCountrylist
之后缺少了
。/GetCountrylist
,当我的更正被接受时,这将被修复。顺便问一句,你的意思是“CityId”=1还是“CityId”:“1”?谢谢我的意思是“CityId”:1它真的是
ContryCode
?:(如果我过度编辑,我深表歉意。一般来说,我认为假设属性名没有空格是安全的,特别是考虑到您有OP中的示例JSON。
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
});