I would like to use this API
https://restcountries.com/#api-endpoints-v3-all
This is the link
https://restcountries.com/v3.1/all
In this object, countries are not alphabetically sorted. How can I do it within the function?
const btn = document.querySelector("button");
const output = document.querySelector("#output");
const intake = document.querySelector("input");
const url = "https://restcountries.com/v3.1/all";
let myData = {};
fetch(url).then(function (res) {
return res.json()
}).then(function (data) {
myData = data;
buildSelect(data);
})
function buildSelect(d) {
let select = document.createElement('select');
d.forEach(function (item, index) {
let option = document.createElement('option');
console.log(item,index);
option.value = index;
option.textContent = item.name.common;
select.appendChild(option);
})
document.querySelector('body').appendChild(select);
}
The code belongs to Laurence Svekis, I’m doing his course, but in his version it’s sorted by API side already.
Thank you!