小工具知识网

当前位置: 首页 > 行业知识 > vue通过年-月-日,年-月,年搜索数据

行业知识

vue通过年-月-日,年-月,年搜索数据

2026-10-31 14:47:14 来源:互联网转载

话不多说,index.php/tags-41973.html" >���接进入正题

()

因为我的搜索是搜索昨日,上月,本月,本年的数据

所以在搜索之前需要先获取到昨日,上月,本月,本年的日期,如下:

()
function getDate() {    var lastMonth = new Date(      new Date().getFullYear(),      new Date().getMonth() - 1,      new Date().getDate()    ); // 获取上个月日期    var currentMonth = new Date().getMonth() + 1; // 获取本月    date.year = new Date().getFullYear(); // 获取本年    date.lastYear = new Date().getFullYear() -1;    date.lastMonth = lastMonth.toISOString().slice(0, 7); // 格式化上个月日期    date.month = date.year + '-' + (currentMonth  

date是我已经定义好的一个reactive

  const date = reactive({    yesterday: '',    lastMonth: '',    month: '',    year: '',    lastYear:''  });

接下来开始搜索

data_date的格式一直都是xxxx-xx-xx

1,首先是搜索昨日的

比如说昨日的数据为:2024-02-27,变量名为date.yesterday

这个arr就是你要搜索的数组

 let yeArr = arr.filter((item) => item.data_date == date.yesterday); //根据自己的date.yesterday与数组中的data_date进行比较,把相等的筛选出来 //此时这个yeArr就是筛选后的数组

2,接下来是搜索上个月的数据,变量名为date.lastMonth,

还是跟arr中每一项的data_date进行匹配

var yearMonth = date.lastMonth.split('-').map(Number);    var filteredDateObjects = arr.filter(function (obj) {      var dateYearMonth = obj.data_date.split('-').slice(0, 2).map(Number);      return dateYearMonth[0] === yearMonth[0] && dateYearMonth[1] === yearMonth[1];    });     //最后搜索到的数据为filteredDateObjects

3,本月

var nowMonth = date.month.split('-').map(Number);    var filteredDateObjects = arr.filter(function (obj) {      var dateYearMonth = obj.data_date.split('-').slice(0, 2).map(Number);      return dateYearMonth[0] === nowMonth[0] && dateYearMonth[1] === nowMonth[1];    });    //最后搜索到的数据为filteredDateObjects

4,本年

 var testYear = arr.filter(function (obj) {      var objYear = obj.data_date.split('-')[0];      return objYear == date.year;      //这个date.year就是我们当前的年份    });

5,去年

去年跟今年同理

 var lastsYear = arr.filter(function (obj) {      var objYear = obj.data_date.split('-')[0];      return objYear == date.lastYear;     });

over,有用的话留下个免费的双击关注吧~

上一篇:C++初阶学习第四弹——类与对象(中)——刨析类与对象的核心点

下一篇:baidu怎么设置服务器?