I've been googling for the past hour and can't seem to find any posts relevant to my question.
I'm trying to loop over all the results from an API request and map it accordingly as a select option.
Here's my code:
<script>
var urlRoot = 'https://newsapi.org/v2/';
var sourceList = 'sources?';
var apiLanguage = 'language=en&';
var apiKey = 'XXX';
$(document).ready( function() {
$.ajax({
url: urlRoot + sourceList + apiLanguage + apiKey,
method: 'GET',
dataType: 'json',
}).then(function(data) {
console.log(data.sources);
$.each(data, function(sources, name) {
$('#source-selection').append(name);
})
});
});
</script>
<select>
<option> Show Everything </option>
<option id="source-selection"></option>
</select>
The API returns the sources as an array of ~80.
Thanks in advance and sorry for bad english.