我有一个这种形式的JSON数组
var data = [{"transDate":new Date("01/01/2018"),"temperature":95},
{"transDate":new Date("02/01/2018"),"temperature":62},
{"transDate":new Date("03/01/2018"),"temperature":51},
{"transDate":new Date("04/01/2018"),"temperature":35},
{"transDate":new Date("05/01/2018"),"temperature":95},
{"transDate":new Date("06/01/2018"),"temperature":62},
{"transDate":new Date("07/01/2018"),"temperature":51},
{"transDate":new Date("08/01/2018"),"temperature":35},
{"transDate":new Date("09/01/2018"),"temperature":95},
{"transDate":new Date("10/01/2018"),"temperature":62},
{"transDate":new Date("11/01/2018"),"temperature":51},
{"transDate":new Date("12/01/2018"),"temperature":35},
{"transDate":new Date("13/01/2018"),"temperature":95},
{"transDate":new Date("14/01/2018"),"temperature":62},
{"transDate":new Date("15/01/2018"),"temperature":51},
{"transDate":new Date("16/01/2018"),"temperature":35},
{"transDate":new Date("17/01/2018"),"temperature":95},
{"transDate":new Date("18/01/2018"),"temperature":62},
{"transDate":new Date("19/01/2018"),"temperature":51},
{"transDate":new Date("20/01/2018"),"temperature":35},
{"transDate":new Date("21/01/2018"),"temperature":95},
{"transDate":new Date("22/01/2018"),"temperature":62},
{"transDate":new Date("23/01/2018"),"temperature":51},
{"transDate":new Date("24/01/2018"),"temperature":35},
{"transDate":new Date("25/01/2018"),"temperature":95},
{"transDate":new Date("26/01/2018"),"temperature":62},
{"transDate":new Date("27/01/2018"),"temperature":51},
{"transDate":new Date("28/01/2018"),"temperature":35}
];
现在我有一个这种形式的日期范围
01/01/2018 - 01/15/2018
我想从这个数据范围之间获取json数组中的所有数据。那么我如何使用切片函数做到这一点?
答案 0 :(得分:0)
使用filter
并将您的日期与startDate
和endDate
值进行比较。使用splice
并不能真正实现所需的结果。
var data = [{"transDate":new Date("01/01/2018"),"temperature":95},
{"transDate":new Date("02/01/2018"),"temperature":62},
{"transDate":new Date("01/10/2018"),"temperature":51},
{"transDate":new Date("04/01/2018"),"temperature":35},
{"transDate":new Date("05/01/2018"),"temperature":95},
{"transDate":new Date("06/01/2018"),"temperature":62},
{"transDate":new Date("07/01/2018"),"temperature":51},
{"transDate":new Date("08/01/2018"),"temperature":35},
{"transDate":new Date("09/01/2018"),"temperature":95},
{"transDate":new Date("10/01/2018"),"temperature":62},
{"transDate":new Date("11/01/2018"),"temperature":51},
{"transDate":new Date("12/01/2018"),"temperature":35},
{"transDate":new Date("13/01/2018"),"temperature":95},
{"transDate":new Date("14/01/2018"),"temperature":62},
{"transDate":new Date("15/01/2018"),"temperature":51},
{"transDate":new Date("16/01/2018"),"temperature":35},
{"transDate":new Date("17/01/2018"),"temperature":95},
{"transDate":new Date("18/01/2018"),"temperature":62},
{"transDate":new Date("19/01/2018"),"temperature":51},
{"transDate":new Date("20/01/2018"),"temperature":35},
{"transDate":new Date("21/01/2018"),"temperature":95},
{"transDate":new Date("22/01/2018"),"temperature":62},
{"transDate":new Date("23/01/2018"),"temperature":51},
{"transDate":new Date("24/01/2018"),"temperature":35},
{"transDate":new Date("25/01/2018"),"temperature":95},
{"transDate":new Date("26/01/2018"),"temperature":62},
{"transDate":new Date("27/01/2018"),"temperature":51},
{"transDate":new Date("28/01/2018"),"temperature":35}
];
var startDate = '01/01/2018';
var endDate = '01/15/2018';
var res = data.filter(dateObj => new Date(startDate) <dateObj.transDate && dateObj.transDate<new Date(endDate));
console.log(res);
&#13;
答案 1 :(得分:0)
那么我如何使用切片功能呢?
要使slice
生效,您需要起始索引和结束索引的索引。使用findIndex
表示相同的内容( 假设数据按日期按升序排序 )
var startDate = "01/01/2018";
var endDate = "01/15/2018";
var fnIsGreaterOrEqual = (dateObj, dateStr) => {}; //logic for date comparison
var startIndex = data.findIndex( s => fnIsGreaterOrEqual(s.transDate, startDate) );
var endIndex = data.findIndex( s => fnIsGreaterOrEqual(s.transDate, endDate) );
var output = data.slice(startIndex, endIndex);
但是,推荐的解决方案将使用filter
本身
var fnToDateObj = (dateStr) => {}; //logic for date conversion
var startDate = fnToDateObj("01/01/2018");
var endDate = fnToDateObj("15/01/2018");
var output = data.filter( s => s.transDate >= startDate && s.transDate <= endDate );