JavaScript根据周和月的排序日期来总结金额

时间:2017-08-26 12:17:52

标签: javascript arrays sorting

我试图对本周,上周,本月和上个月的数据进行排序。同样的数据:

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

2 个答案:

答案 0 :(得分:2)

要按日期排序,请使用ScreenToWorldPoint,它会对其进行排序,因此您不需要其他数组:

arr.sort()

如果您喜欢短代码,请使用箭头功能:

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

然后,为了计算这个/上周/月,请使用moment.js

arr.sort((a,b)=>new Date(a.date)-new Date(b.date));
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]);

答案 1 :(得分:0)

你不需要排序。您只需要检查当天,昨天或本月,并在迭代时总结值:

 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 );