简体   繁体   中英

Join or lookup two JSON dataset

i have two JSON data of following structure that i get from server using 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 },
    .
    .
    .
]

I need to display City, Country in my div how do i lookup the countrCode from countrylist in Citylist Object and put its full Country. So i can display

Abernant, United States
Academy Park, Belgium
Abernathy, Argentina

i have this code so far

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

You can transform countrylist using countrylist.Code as a property. Making getting the country a trivial task.

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

Now, you can iterate over citylist and get the country from countries .

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

See it here.

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

Here is a possible solution using $.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/

You can do it with Alasql JavaScript library.

This is a main operator:

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

Try this sample at jsFiddle.

Alasql can download JSON data directly to SELECT statement:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM