I'm fetching a list of countries from the API and I want to display them in groups by subregion (continent). Like that:
API gives me a response which is an array of objects (countries) and for each object there is a key called 'subregion' - I want to group by that key. I use lodash for grouping, but maybe there is a Vue method I'm not familiar with. My JS code:
var app = new Vue({
el: '#app',
data: {
countries: null,
},
created: function () {
this.fetchData();
},
methods: {
fetchData: function() {
var xhr = new XMLHttpRequest();
var self = this;
xhr.open('GET', apiURL);
xhr.onload = function() {
var countryList = JSON.parse(xhr.responseText);
self.countries = _.groupBy(countryList, "subregion");
};
xhr.send();
},
},
});
And HTML:
<li v-for="country in countries">
{{ country.name }} (+{{ country.callingCodes[0] }})
</li>
How can I achieve what's in the picture?
