我有一个像这样的json数组。
var data=[
{
Title:"Dummy1",
Events:[{
eventName:"StartDate"
Date:"Jan-2014"
},
{
eventName:"EndDate"
Date:"Feb-2015"
}]
},
{
Title:"Dummy2",
Events:[{
eventName:"StartDate"
Date:"Jan-2013"
},
{
eventName:"EndDate"
Date:"Feb-2015"
}]
},
{
Title:"Dummy3",
Events:[{
eventName:"StartDate"
Date:"Feb-2014"
},
{
eventName:"EndDate"
Date:"Mar-2015"
}]
}];
请注意,日期不是日期对象。他们是字符串。
我需要找到StartDate的最小值和EndDate的最大值 所以结果应该是
MinStartDate:Jan-2013
MaxEndDate:Mar-2015
我更愿意使用角度,如果不是javascript或jquery也没关系。
提前感谢。
答案 0 :(得分:2)
有我的解决方案,这有点长,但很容易理解和编辑任何更新。 我确信我们可以用4-5行来解决它,但无论如何,它都有效。
首先,定义一个月份数组:
var month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
每次需要在数组中搜索最小值/最大值时,需要使用第一个值初始化min和max:
var max_end_date = data[0].Events[1].Date;
var min_start_date = data[0].Events[0].Date;
然后,将每个比较年/月的日期拆分为start en end,你得到如下结果:
$.each(data, function(k, v) {
var month_min_start_date = min_start_date.split("-")[0];
var year_min_start_date = min_start_date.split("-")[1];
var current_start_date = v.Events[0].Date;
var month_current_start_date = current_start_date.split("-")[0];
var year_current_start_date = current_start_date.split("-")[1];
if (year_current_start_date < year_min_start_date
|| year_current_start_date == year_min_start_date && month.indexOf(month_current_start_date) < month.indexOf(month_min_start_date)) {
min_start_date = current_start_date;
}
var month_max_end_date = max_end_date.split("-")[0];
var year_max_end_date = max_end_date.split("-")[1];
var current_end_date = v.Events[1].Date;
var month_current_end_date = current_end_date.split("-")[0];
var year_current_end_date = current_end_date.split("-")[1];
if (year_current_end_date > year_max_end_date
|| year_current_end_date == year_max_end_date && month.indexOf(month_current_end_date) > month.indexOf(month_max_end_date)) {
max_end_date = current_end_date;
}
})
您可以在JSFiddle
上试用答案 1 :(得分:1)
这是使用Math
var data=[
{
Title:"Dummy1",
Events:[{
eventName:"StartDate",
Date:"Jan-2014"
},
{
eventName:"EndDate",
Date:"Feb-2015"
}]
},
{
Title:"Dummy2",
Events:[{
eventName:"StartDate",
Date:"Jan-2013"
},
{
eventName:"EndDate",
Date:"Feb-2015"
}]
},
{
Title:"Dummy3",
Events:[{
eventName:"StartDate",
Date:"Feb-2014"
},
{
eventName:"EndDate",
Date:"Mar-2015"
}]
}];
var startDates = [],
endDates = [];
for (i = 0; i < data.length; i++) {
startDates.push(Date.parse(data[i].Events[0].Date));
endDates.push(Date.parse(data[i].Events[1].Date));
}
Array.min = function( array ){
return Math.min.apply( Math, array );
};
Array.max = function( array ){
return Math.max.apply( Math, array );
};
// or use a library like Moment.js to format dates
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var minDate = new Date(Array.min(startDates)),
minDateFormat = months[minDate.getMonth()]+'-'+minDate.getFullYear();
var maxDate = new Date(Array.max(endDates)),
maxDateFormat = months[maxDate.getMonth()]+'-'+maxDate.getFullYear();
console.log(minDateFormat );
console.log(maxDateFormat);
&#13;
答案 2 :(得分:0)
我创建了一个名为dataSort()
的函数,并对数据数组ASC OR DESC顺序进行了排序。由于我按字段对数组进行了排序,因此很容易选择最小值或最大值。
<script>
var data = [
{
Title: "Dummy1",
Events: [{
eventName: "StartDate",
Date: "Jan-2014"
},
{
eventName: "EndDate",
Date: "Feb-2015"
}]
},
{
Title: "Dummy2",
Events: [{
eventName: "StartDate",
Date: "Jan-2013"
},
{
eventName: "EndDate",
Date: "Feb-2015"
}]
},
{
Title: "Dummy3",
Events: [{
eventName: "StartDate",
Date: "Feb-2014"
},
{
eventName: "EndDate",
Date: "Mar-2015"
}]
}];
function dataSort(jsonData, orderBy, order) {
var index = 0; // 0 StartDate
if (orderBy == 'EndDate') {
index = 1; // 1 EndDate
}
jsonData.sort(function (a, b) {
var ad = a.Events[index].Date;
ad = ad.replace(new RegExp('-'), '1, ')
aStartdate = new Date(Date.parse(ad));
var bd = b.Events[index].Date;
bd = bd.replace(new RegExp('-'), '1, ')
bStartdate = new Date(Date.parse(bd));
if (order == 'ASC') {
return aStartdate - bStartdate; // asc
} else {
return bStartdate - aStartdate; // desc
}
});
return jsonData;
}
var dataSortByStartDate = dataSort(data, 'StartDate', 'ASC');
var MinStartDate = dataSortByStartDate[0].Events[0].Date;
console.log('MinStartDate: ' + MinStartDate);
var dataSortByEndDate = dataSort(data, 'EndDate', 'DESC');
var MaxEndDate = dataSortByEndDate[0].Events[1].Date;
console.log('MaxEndDate: ' + MaxEndDate);