2013-01-13 16 views
0

我有以下結構 的兩個JSON數據,我使用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對象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 
    }); 
+0

你知道你已經乘缺少qoutes(''')吧? – andlrc

+0

乘?不確定 –

+0

我只是修正了這些 – MyStream

回答

1

您可以使用countrylist.Code作爲屬性將countrylist轉換。讓國家變成一件小事。現在

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

,你可以遍歷citylistcountries拿到國內。

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

See it here.

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

我很抱歉,如果我超編輯。一般來說,我認爲可以安全地假設屬性名稱不會有空格,特別是考慮到你有來自OP的示例JSON。 – ErikE

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/

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 this sample

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 
}); 
相關問題