这几天在项目开发中遇到需要将后台返回的列表数据根据一定的条件筛选后进行倒序排列,因此我做了如下处理:
代码中使用了filter过滤及sort排序函数
sort()排序函数详解:
引用详情
// 置顶排序
const topData = this.annountNoticeList.filter(item => item.topFlag == -1).sort((a,b) => b.startTime - a.startTime); // 过滤topFlag为-1的数据后,使用了sort函数进行到序排列
const nonTopData = this.annountNoticeList.filter(item => item.topFlag == 0).sort((a,b) => b.startTime - a.startTime); // 过滤topFlag为0的数据后,使用了sort函数进行到序排列
const sorteData = [...topData, ...nonTopData] // 将两个数据合并
this.annountNoticeList = sorteData; // 赋值给渲染列表