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.