JavaScript 根据周和月对日期进行排序以求和金额

标签 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

最佳答案

要按日期排序,请使用 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>

关于JavaScript 根据周和月对日期进行排序以求和金额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45895263/

相关文章:

javascript - 适应浏览器窗口的高度

javascript - 无法在 Javascript 中将数组按升序排序

mysql - 合并mysql中的值并排序

sorting - 类别值的elasticsearch排序聚合

Javascript 按字母顺序排序;如何将数值与数字进行比较,同时将它们与字符串进行比较?

javascript - 我的 clearTimeout 函数不起作用

javascript - 在另一个变量的标题中使用来自一个变量的数据 - javascript

返回 "n"给定颜色的 JavaScript 函数(从深到浅)

php - 生成数组之间元素的所有组合

c++ - 读取字符并创建数组c++