簡體   English   中英

連接或查找兩個JSON數據集

[英]Join or lookup two JSON dataset

我有兩個以下結構的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中顯示城市,國家/地區,如何從Citylist對象的countrylist中查找countrCode並將其完整的Country放入。 所以我可以展示

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

您可以使用countrylist.Code作為屬性來轉換countrylist 使這個國家變得微不足道。

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

現在,您可以遍歷citylist並從countries地區獲得countries

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

在這里看到它。

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

這是使用$.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/

您可以使用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
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM