I tried to print the countries using US Dollar in Restcountries API (https://restcountries.com/v3.1/all
). I tried but nothing shows as an output:
My code as follows
const getUSDollar = () => {
const xhr = new XMLHttpRequest();
xhr.open("GET", "https://restcountries.com/v3.1/all", true);
xhr.responseType = "json";
xhr.onload = () => {
const data = xhr.response;
const datas = data.filter((value) => {
for (var i in value.currencies.name.USD) {
if (i === "Unites States Dollar") {
return true;
}
}
}).map((value) => value.name);
console.log(datas);
}
xhr.send();
};
getUSDollar();
Please look into this.
CodePudding user response:
Can you try this
xhr.onload = () => {
const data = xhr.response;
const datas = data
.filter((item) => item?.currencies?.USD?.name === "United States dollar")
.map((value) => value.name);
console.log('datas', datas);
};
As @eglease suggested, you don't need to check the name, you can just check for USD and filter the obj like this,
xhr.onload = () => {
const data = xhr.response;
const datas = data
.filter((item) => item?.currencies?.USD)
.map((value) => value.name);
console.log('datas', datas);
};
It worked for me. Try and comment whether this is what you are expected or not?