I am going through an array , in my VUE app, that has the following structure
[
{
"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
}
]
}
]
the first thing I'm doing to show the different options of the select where I show them is a filter by 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>
up to this point I manage to show the brands corresponding to each id in the select, but I wanted to sort them also in alphabetical order and I can't find a way to combine the filter with a sort. I have tried to concatenate the methods but it returns a syntax error.
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;
});
}
}
How can I do it? Thank you all for your time and help in advance
sortcode you've shown. (I wouldn't use that code, but I don't see a syntax error in it.)