Extracting data from an API that provides information in the format below:
[
{
"id": 173,
"date": "2020-12-10T16:05:30",
"date_gmt": "2020-12-10T16:05:30",
"guid": {},
"modified": "2020-12-10T16:05:31",
"modified_gmt": "2020-12-10T16:05:31",
"slug": "test",
"status": "publish",
"type": "place",
"link": "http://localhost:81/test/",
"title": {},
"content": {},
"featured_media": 0,
"template": "",
"acf": {
"address": {
"address": "123 Test Address",
"street_number": "123",
"street_name": "Test Address",
"city": "Philipsburg",
"state": "Sint Maarten",
"country": "Sint Maarten",
"country_short": "SX"
},
"header": {}
},
"_links": {}
},
etc
]
Saving it in Vuex, and structuring the data with this approach:
computed: {
resorts() {
const resorts = {};
if (this.$store.state.loading === false) {
this.$store.state.posts.forEach((post) => {
const c = post.acf.address.country;
const s = post.acf.address.state;
//const t = post.title;
resorts[c] = resorts[c] || {};
resorts[c][s] = resorts[c][s] || [];
resorts[c][s].push(post);
});
}
return resorts;
},
}
Rendering the data in a v-for
loop using Pug:
section.united-states(v-for="(country, index) in resorts" v-if="index==='United States'")
h1(v-html="index")
section.state(v-for="(state, subIndex) in country" :key="subIndex" :class="subIndex.toLowerCase()")
h5(v-html="subIndex")
ul
li(v-for="post, resort) in state")
listing(:id="post.id" :slug="post.slug" :image="post.acf.header" :title="post.title.rendered" :city="post.acf.address.city" :street="post.acf.address.street_name_short")
The current display is correct. However, I aim to sort it alphabetically by Country, followed by State, and then City names. I've experimented with sorting methods like lodash.orderBy
, but was unable to achieve the desired organization. The Vue inspector reveals that countries and states (not cities) are listed alphabetically. Any suggestions?