我正在尝试从数组中排序本周,上周,本月和上个月的利润。相同的数据:
var arr = [{date: '2017/12/16', profit: 12.50},
{date: '2017/05/01', profit: 13.50},
{date: '2017/04/20', profit: 14.50},
{date: '2017/03/10', profit: 15.50},
{date: '2017/08/15', profit: 16.50},
{date: '2017/08/16', profit: 26.50},
{date: '2017/08/24', profit: 16.50},
{date: '2017/08/25', profit: 36.50},
{date: '2017/03/06', profit: 17.50},
{date: '2017/02/04', profit: 18.50},
{date: '2017/01/07', profit: 19.50}];
我想根据本周/上周/本月/上个月进行排序并获取利润。我试图用今天和昨天的利润来获得:
var today = getTodayDate();
var todayProfit = 0;
for(var i = 0; i < arr.length; i++){
if(arr[i].date == today){
todayProfit = arr[i].total;
break;
}
}
昨天也一样,我基本上得到了昨天的日期。我如何实际排序本周/上周/本月/上个月?我是否必须创建另一个数组以获取所有日期并嵌套for循环?
有没有更好的办法?先谢谢了!
更新
所需的输出:
This week profit: 53.00
Last week profit: 43.00
This month profit: 96.00
Last month profit: 0.00
最佳答案
要按日期排序,请使用arr.sort()
,它将在适当的位置对其进行排序,因此您不需要其他数组:
arr.sort(function(a,b){
return new Date(a.date)-new Date(b.date);
});
如果您喜欢短代码,请使用箭头功能:
arr.sort((a,b)=>new Date(a.date)-new Date(b.date));
然后,要计算此/上周/月,请使用moment.js:
var arr = [
{date: '2017/12/16', profit: 12.50},
{date: '2017/05/01', profit: 13.50},
{date: '2017/04/20', profit: 14.50},
{date: '2017/03/10', profit: 15.50},
{date: '2017/08/15', profit: 16.50},
{date: '2017/08/16', profit: 26.50},
{date: '2017/08/24', profit: 16.50},
{date: '2017/08/25', profit: 36.50},
{date: '2017/03/06', profit: 17.50},
{date: '2017/02/04', profit: 18.50},
{date: '2017/01/07', profit: 19.50}
];
var profits = [0,0,0,0]; // this/last week, this/last month
var date;
function isThisWeek(d) {
// start and end of this week
var thisWeek = [moment().utc().startOf('week'),
moment().utc().endOf('week')];
return d.isBetween(thisWeek[0],thisWeek[1])||
d.isSame(thisWeek[0])||
d.isSame(thisWeek[1]);
}
function isLastWeek(d) {
// start and end of this week minus 1, which is last week
var lastWeek = [moment().utc().subtract(1,'weeks').startOf('week'),
moment().utc().subtract(1,'weeks').endOf('week')];
return d.isBetween(lastWeek[0],lastWeek[1])||
d.isSame(lastWeek[0])||
d.isSame(lastWeek[1]);
}
function isThisMonth(d) {
// start and end of this month
var thisMonth = [moment().utc().startOf('month'),
moment().utc().endOf('month')];
return d.isBetween(thisMonth[0],thisMonth[1])||
d.isSame(thisMonth[0])||
d.isSame(thisMonth[1]);
}
function isLastMonth(d) {
// start and end of this month minus 1, which is last month
var lastMonth = [moment().subtract(1,'months').utc().startOf('month'),
moment().subtract(1,'months').utc().endOf('month')];
return d.isBetween(lastMonth[0],lastMonth[1])||
d.isSame(lastMonth[0])||
d.isSame(lastMonth[1]);
}
arr.forEach(function(e){
date=moment.utc(e.date,'YYYY-MM-DD');
if (isThisWeek(date)) { // if it's this week
profits[0]+=e.profit;
} else if (isLastWeek(date)) { // if it's last week
profits[1]+=e.profit;
}
if (isThisMonth(date)) { // if it's this month
profits[2]+=e.profit;
} else if (isLastMonth(date)) { // if it's last month
profits[3]+=e.profit;
}
});
console.log("This week profits : "+profits[0]);
console.log("Last week profits : "+profits[1]);
console.log("This month profits : "+profits[2]);
console.log("Last month profits : "+profits[3]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>