1

I’m a new programmer, but I read the jsonQ documentation and do not see where it helps me with my question.

I have some API data as follows:

{"success": true,"terms": "https://currencylayer.com/terms","privacy":      "https://currencylayer.com/privacy","currencies": {"AED": "United Arab Emirates Dirham","AFN": "Afghan Afghani","ALL": "Albanian Lek","AMD": "Armenian Dram","ANG": "Netherlands Antillean Guilder","AOA": "Angolan Kwanza","ARS": "Argentine Peso","AUD": "Australian Dollar","AWG": "Aruban Florin","AZN": "Azerbaijani Manat","BAM": "Bosnia-Herzegovina Convertible Mark","BBD": "Barbadian Dollar","BDT": "Bangladeshi Taka","BGN": "Bulgarian Lev","BHD": "Bahraini Dinar","BIF": "Burundian Franc","BMD": "Bermudan Dollar","BND": "Brunei Dollar","BOB": "Bolivian Boliviano","BRL": "Brazilian Real","BSD": "Bahamian Dollar","BTC": "Bitcoin","BTN": "Bhutanese Ngultrum","BWP": "Botswanan Pula","BYN": "New Belarusian Ruble","BYR": "Belarusian Ruble","BZD": "Belize Dollar","CAD": "Canadian Dollar", }}

(this is a shortened list of all the currencies)

I need to sort alphabetically by currency name (ex: Argentine Peso) and append currency code/key in front of the currency name(ex: ARS Argentine Peso). Can this be done with javascript alone? That is what I prefer.

Here is my code: can you please help with how to sort and append this way, and how to incorporate it into my existing code? I tried using "obj.sort;

$.getJSON("http://apilayer.net/api/list?  access_key=xxxxxxxxxxxxxxxxxxxxx&prettyprint=1", function(data){
  // grab a reference to the second select element
  var countryList2 = document.getElementById("countries2");

  // store the currencies object from the API in a variable
  var obj = data.currencies;

  // loop through the object again to populate the second list
  for (var prop in obj) {
    if (obj.hasOwnProperty(prop)) {
      var opt = document.createElement("option");
      opt.innerHTML = obj[prop];
      opt.value = obj[prop];
      countryList2.appendChild(opt);   /*appended to a select box: <p>To<select id="countries2"></select></p>*/
    }
  }




});

Thank you very much for any assistance.

Wes Foster
  • 8,770
  • 5
  • 42
  • 62

1 Answers1

0

Here's how you can map your object into an array and sort it afterwards:

   // given the currencies object you have provided
   var currencies = {
     "AED": "United Arab Emirates Dirham",
     "AFN": "Afghan Afghani",
     "ALL": "Albanian Lek",
     "AMD": "Armenian Dram",
     "ANG": "Netherlands Antillean Guilder",
     "AOA": "Angolan Kwanza",
     "ARS": "Argentine Peso",
     "AUD": "Australian Dollar",
     "AWG": "Aruban Florin",
     "AZN": "Azerbaijani Manat",
     "BAM": "Bosnia-Herzegovina Convertible Mark",
     "BBD": "Barbadian Dollar",
     "BDT": "Bangladeshi Taka",
     "BGN": "Bulgarian Lev",
     "BHD": "Bahraini Dinar",
     "BIF": "Burundian Franc",
     "BMD": "Bermudan Dollar",
     "BND": "Brunei Dollar",
     "BOB": "Bolivian Boliviano",
     "BRL": "Brazilian Real",
     "BSD": "Bahamian Dollar",
     "BTC": "Bitcoin",
     "BTN": "Bhutanese Ngultrum",
     "BWP": "Botswanan Pula",
     "BYN": "New Belarusian Ruble",
     "BYR": "Belarusian Ruble",
     "BZD": "Belize Dollar",
     "CAD": "Canadian Dollar",
   };

   // create empty array to which currencies will be mapped as an objects array
   var friendlyCurrencies = [];

   // map each currency to 2-property-objects
   for (var currency in currencies) {
     friendlyCurrencies.push({
       currencyCode: currency,
       currencyName: currencies[currency]
     });
   };

   // sort currencies by currencyName (case-insensitive)
   friendlyCurrencies.sort(function(a, b) {
     return a.currencyName.toLowerCase().localeCompare(b.currencyName.toLowerCase());
   });

   // alert first and last currency (ordered by currency name)
   alert('first currency: ' + friendlyCurrencies[0].currencyName + '\n' + 'last currency: ' + friendlyCurrencies[friendlyCurrencies.length-1].currencyName);
 });

Here's a working plunkr with the code you can see above

Adjust the code to your needs.

Andrew B
  • 860
  • 1
  • 14
  • 23