Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMartin Coutts
18,154 PointsTrying to get API data to fill select item
I am trying to make an API call, return a list of currencies and then populate a select menu with this list however I cannot seem to do anything with the data I have returned. I can store it in an array but not console log it, use a loop to iterate through it or anything
this is the data I am getting back https://free.currencyconverterapi.com/api/v6/currencies
here is the repo for my project if you need to see the whole thing https://github.com/martincavaliers/myCurrencyConverter/tree/apiIntegrationJquery
Only a beginner with API's but I feel it is something do with the nesting of the returned objects
Any help would be greatly appreciated
2 Answers
James Anwyl
Full Stack JavaScript Techdegree Graduate 49,960 PointsHi Martin,
Looks like you have a syntax error in your index.html
<script src = "app.js"></script>
Should be:
<script src="app.js"></script>
If you change that, and then uncomment console.log(data); on line 14 of app.js. You should see the data being returned.
Hope this helps :)
James Anwyl
Full Stack JavaScript Techdegree Graduate 49,960 PointsHi Martin,
Sorry I thought you meant you couldn't console.log it.
Important to note that the data we want is 4 levels deep. For example, to access the currency name we could do:
data.results.currency.currencyName
With that in mind, you could try something like:
function createArray(api, array) {
$.getJSON(api, function(data) {
console.log(data.results);
// Loop over each item within data.results.
$.each(data.results, function(index, currency) {
// console.log("currency: ", currency);
// Create a new option tag with currency name as the value attribute
var currencyOption = '<option value="' + currency.currencyName + '">';
// If theres a currency symbol, use it.
if (currency.currencySymbol) {
currencyOption += currency.currencySymbol;
currencyOption += ' ';
}
// Add currency name + close tag
currencyOption += currency.currencyName;
currencyOption += '</option>';
// Append currencyOption tag to #currentCurrencyType select
$('#currentCurrencyType').append(currencyOption);
});
})
}
The code is saying:
Loop over each currency within data.results.
Then construct a HTML option tag using the currencyName and, if it exists, the currencySymbol.
Finally, append the option to the #currentCurrencyType select element.
Uncomment the console.log's so you can see what's happening.
See jQuery each()
Hope this helps :)
Martin Coutts
18,154 PointsMartin Coutts
18,154 PointsI can console log data but it's trying to loop through the results from data I'm stuck on.
Thanks for your help though : ), hit a bit of a wall with this one.