我有一个 API,它以以下格式返回数据:
[
{
"id": 12,
"acf": {
"address": {
"city": "Bandar Penawar",
"state": "Johor",
"country": "Malaysia",
}
},
{
"id": 16,
"acf": {
"address": {
"city": "Some City",
"state": "Arizona",
"country": "United 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);
},
},
这将返回两个单独的数组,countries并且states,如何按国家/地区以及该国家/地区内的州组织数组?
慕哥6287543
相关分类