对过滤后的数组进行排序

问题描述 投票:0回答:1

我知道它是.sort(),但我不知道它在下面的代码中的位置。我已经尝试了所有的方法,它要么坏了,要么不工作。在这种情况下,我想按字母顺序排序。

<div id="navBar">
  <span v-for="country in countries" :key="country">
    <button v-on:click="setCountry(country)">{{ country }}</button>       
  </span>
</div>

data: {  
selectedCountry: ""
},

computed: {
countries() {
  return [...new Set(this.hotels.map(h => h.country))]
}
},
methods: {
setCountry(country) {
  this.selectedCountry = country === this.selectedCountry
    ? ''
    : country;
  this.selectedRegions = [];
  }
},
vue.js
1个回答
0
投票

我想这可能有用

computed: {
countries() {
  let arr = [...new Set(this.hotels.map(h => h.country))]
  return arr.sort()
}

此外,如果这不起作用,那么您的 [...new Set(this.hotels.map(h => h.country))] 看起来像。

© www.soinside.com 2019 - 2024. All rights reserved.