【发布时间】:2021-03-16 20:15:41
【问题描述】:
我有一个 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,我如何按国家/地区组织数组,然后按该国家/地区的州组织?
【问题讨论】:
-
你想要什么格式的数据?
-
到目前为止,嵌套数组似乎可以完美运行!我不知道如何在相应的州后面加上他们的国家。
标签: javascript json vue.js vuejs2 vue-component