簡體   English   中英

JavaScript按周和月對日期進行排序以匯總金額

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

我正在嘗試從數組中排序本周,上周,本月和上個月的利潤。 相同的數據:

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> 

您不需要排序。 您只需要檢查當前日期,昨天或本月,並在進行迭代時總結這些值:

 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