javascript - 重新排序计算数组上的索引?
问题描述
我已经在线搜索了文档和问题,但找不到如何修改我的计算数组的索引顺序。关于从哪里开始的任何想法?
我有一组“优惠”对象,我想在典型的升序或降序选择框中按“offer.price.from”排序。
<!-- SORT BY SELECT -->
<select v-model="sortby" >
<option value="ascending"> Sort: Price ascending</option>
<option value="descending"> Sort: Price descending</option>
</select>
<div v-for="offer in filteredOffers" class="grid-4 item hotel hotel-item">
<!-- OFFER CONTENT HERE -->
</div>
computed: {
filteredOffers () {
return this.offers.filter(offer => {
return (offer.island === this.filters.islandFilter || this.filters.islandFilter === 'All') // Island
&& (offer.starrating === this.filters.starRating || this.filters.starRating === 'All') // Star Rating
&& (offer.board === this.filters.boardBasis || this.filters.boardBasis === 'All') // Board Basis
&& (offer.duration === this.filters.duration || this.filters.duration === 'All') // Duration
&& (offer.price.from < this.filters.price) // Price
&& (this.filters.travelby === 'sea' && offer.travel.air === false || this.filters.travelby === 'All') // Sea or Air
// && (this.filters.range.startDate >= offer.dates.start && offer.dates.end <= this.filters.range.endDate)
});
}
}
解决方案
通过对过滤后的数组进行排序并根据sortby
数据项将其作为计算属性返回来尝试此操作:
computed: {
filteredOffers() {
let filtered = this.offers.filter(offer => {
return (offer.island === this.filters.islandFilter || this.filters.islandFilter === 'All') // Island
&&
(offer.starrating === this.filters.starRating || this.filters.starRating === 'All') // Star Rating
&&
(offer.board === this.filters.boardBasis || this.filters.boardBasis === 'All') // Board Basis
&&
(offer.duration === this.filters.duration || this.filters.duration === 'All') // Duration
&&
(offer.price.from < this.filters.price) // Price
&&
(this.filters.travelby === 'sea' && offer.travel.air === false || this.filters.travelby === 'All') // Sea or Air
// && (this.filters.range.startDate >= offer.dates.start && offer.dates.end <= this.filters.range.endDate)
});
if (this.sortby == 'ascending') {
return filtered.sort((a, b) => {
return a.price.form - b.price.form;
})
} else {
return filtered.sort((a, b) => {
return b.price.form - a.price.form;
})
}
}
}
推荐阅读
- android - 从库项目中杀死一个应用程序进程
- reactjs - ReactJS 从另一个组件调用一个函数
- python-3.x - ModuleNotFoundError:没有名为“keybord”的模块
- javascript - VueJS“RangeError:超出最大调用堆栈大小”
- azure - 如何将我的 GoDaddy 域连接到 Azure VM 服务器?
- python - 我无法通过 Django 中的最近聊天来组织我的消息
- java - 重用 Java (Swing) 中已经实现的函数
- reactjs - React Helmet:根据配色方案更改图标
- firebase - Cloud Firestore - how to stop “any user can read your entire database” error email
- react-native - ReactNative:SectionList SectionHeaders 覆盖 ListFooter