I am working with an API that provides data in a specific format:
[
{
"id": 12,
"acf": {
"address": {
"city": "Bandar Penawar",
"state": "Johor",
"country": "Malaysia",
}
},
{
"id": 16,
"acf": {
"address": {
"city": "Some City",
"state": "Arizona",
"country": "United States",
}
}
]
Currently, I have a computed code block to extract the list of countries and states:
computed: {
countries() {
const countries = new Set();
this.$store.state.posts.forEach((post) =>
countries.add(post.acf.address.country)
);
return Array.from(countries);
},
states() {
const states = new Set();
this.$store.state.posts.forEach((post) =>
states.add(post.acf.address.state)
);
return Array.from(states);
},
},
The above script generates separate arrays for countries
and states
. How can I reorganize these arrays by country first, then by states within each country?