【发布时间】:2020-10-07 08:39:44
【问题描述】:
在我的 VUE 应用程序中,我正在查看一个数组,该数组具有以下结构
[
{
"id": 1,
"brands": [
{
"name": "Mall",
"id": 1
},
{
"name": "Tanted",
"id": 25
},
{
"name": "Anteil",
"id": 12
},
{
"name": "Moscard",
"id": 73
}
]
},
{
"id": 2,
"brands": [
{
"name": "Yanre",
"id": 6
},
{
"name": "Alted",
"id": 10
},
{
"name": "Sillex",
"id": 9
},
{
"name": "Straf",
"id": 78
}
]
}
]
我要做的第一件事是显示选择的不同选项,我在其中显示它们是按 id 过滤
computed: {
filteredBrand() {
var selectedBrand =
!this.businessInfo || this.businessInfo.selectedBrand == 0
? 1
: this.businessInfo.selectedBrand;
return !this.$store.getters.brands
? null
: this.$store.getters.brands.filter(
item => item.id == selectedBrand
)[0].brands;
}
}
<select
v-model="businessInfo.selectedBrand"
@change="onChangeBrand($event)">
<option v-for="brand in filteredBrand" v-bind:key="brand.name">{{ brand.name }}</option>
</select>
到目前为止,我设法在选择中显示与每个 id 相对应的品牌,但我也想按字母顺序对它们进行排序,但我找不到将过滤器与排序相结合的方法。 我试图连接这些方法,但它返回一个语法错误。
computed: {
filteredBrand() {
var selectedBrand =
!this.businessInfo || this.businessInfo.selectedBrand == 0
? 1
: this.businessInfo.selectedBrand;
return !this.$store.getters.brands
? null
: this.$store.getters.brands.filter(
item => item.id == selectedBrand
)[0].brands.sort(function(a, b) {
return a.name === b.name ? 0 : +(a.name > b.name) || -1;
});
}
}
我该怎么做? 提前感谢大家的时间和帮助
【问题讨论】:
-
“但它返回语法错误” 请始终包含实际完整错误消息的复制和粘贴,并告诉我们什么它抱怨的线。我在您显示的
sort代码中看不到任何语法错误。 (我不会使用该代码,但我没有看到其中的语法错误。)
标签: javascript vue.js sorting methods filter