简体   繁体   English

JavaScript按周和月对日期进行排序以汇总金额

[英]JavaScript sort date according to week and month to sum up the amount

I am trying to sort the profit for this week, last week, this month and last month from an array. 我正在尝试从数组中排序本周,上周,本月和上个月的利润。 The sameple data: 相同的数据:

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}];

I wanted to sort and get the profit according to this week/last week/this month/last month. 我想根据本周/上周/本月/上个月进行排序并获取利润。 I have tried with getting today and yesterday's profit with this: 我试图用今天和昨天的利润来获得:

var today = getTodayDate();
var todayProfit = 0;

for(var i = 0; i < arr.length; i++){
if(arr[i].date == today){
    todayProfit = arr[i].total;
    break;
}
}

Same goes for yesterday which I basically get the yesterday's date. 昨天也一样,我基本上得到了昨天的日期。 How can I actually sort for this week/last week/this month/last month? 我如何实际排序本周/上周/本月/上个月? Do I have to create another array to get all the dates and do nested for loop? 我是否必须创建另一个数组以获取所有日期并嵌套for循环?

Is there any better approach? 有没有更好的办法? Thanks in advanced! 提前致谢!

UPDATED 更新

Desired output: 所需的输出:

This week profit: 53.00
Last week profit: 43.00
This month profit: 96.00
Last month profit: 0.00

To sort by date, use arr.sort() , which will sort it in place so you won't need another array : 要按日期排序,请使用arr.sort() ,它将对它进行排序,因此您不需要另一个数组:

arr.sort(function(a,b){
    return new Date(a.date)-new Date(b.date);
});

And if you like short code, use an arrow function : 如果您喜欢短代码,请使用箭头功能:

arr.sort((a,b)=>new Date(a.date)-new Date(b.date));

Then, for figuring this/last week/month, use moment.js : 然后,要计算此/上周/一个月,请使用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> 

You dont need to sort. 您不需要排序。 You just need to check for current day, yesterday, or this month, and sum up the values when iterating: 您只需要检查当前日期,昨天或本月,并在进行迭代时总结这些值:

 const today = "2017/08/26", yesterday = " 2017/08/25";
 const month = today.substr(0,-3);

 //results
 let profit = {
  today:0,
  yesterday:0,
  month:0
 };

arr.forEach(({date,profit}) =>{
 if(date === today) profit.today += profit;
 if(date === yesterday) profit.yesterday += profit;
 if(date.substr(0,-3) === month) profit.month += profit;
});

console.log( profit );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM