如何创建日期计数器?我尝试使用减少功能 [英] How to create a date counter ? i tried it using reduce function

查看:84
本文介绍了如何创建日期计数器?我尝试使用减少功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用id,billStatus和tDate需要进行一个daycounter,以计算收到线索的日期。例如=> for id: 1 2 5月8日的应付帐款和5月9日的3天帐单将变成=> 1 + 1 = 2

using id, billStatus and tDate need to make a daycounter which counts date of days leads are received. Example => for id:"1" 2 Billable against 8th of may AND 3 Billable for 9th of may than day counter will become => 1+1 = 2

注意:如果我一天获得1个潜在客户或7个潜在客户,则daycount将为1,并在第二天增加(当新的一天有新的潜在客户)。

Note: if I get 1 lead or 7 leads for a single day daycount will be 1 and increment the next day when there will be any new lead on a new day.

 data: [
      { id: "1", billStatus: "Billable", tDate: "05/08/2020", dayCounter: 0 },
      { id: "1", billStatus: "Billable", tDate: "05/08/2020", dayCounter: 0 },
      { id: "1", billStatus: "Non-Billable", tDate: "05/08/2020", dayCounter: 0 },
      { id: "1", billStatus: "Billable", tDate: "05/09/2020", dayCounter: 6 },
      { id: "1", billStatus: "Non-Billable", tDate: "05/09/2020", dayCounter: 0 },
      { id: "1", billStatus: "Billable", tDate: "05/09/2020", dayCounter: 0 },
      { id: "1", billStatus: "Billable", tDate: "05/09/2020", dayCounter: 0 },
      { id: "3", billStatus: "Billable", tDate: "05/10/2020", dayCounter: 0 },
      { id: "2", billStatus: "Billable", tDate: "05/10/2020", dayCounter: 0 },
      { id: "2", billStatus: "Non-Billable", tDate: "05/10/2020", dayCounter: 0 },
      ],




retult:  
data: [
          { id: "1", dayCounter: 0 },
          { id: "2",  dayCounter: 2 },
          { id: "3", dayCounter: 1 }]


推荐答案

您可以检查此解决方案。尽管我不清楚您的预期输出。但是希望它能为您提供一些想法。

You can check this solution. Though i'm not clear about your expected output. But hope it will provide you some idea.

const data = [
  { id: "1", billStatus: "Billable", tDate: "05/08/2020", dayCounter: 0 },
  { id: "1", billStatus: "Billable", tDate: "05/08/2020", dayCounter: 0 },
  { id: "1", billStatus: "Non-Billable", tDate: "05/08/2020", dayCounter: 0 },
  { id: "1", billStatus: "Billable", tDate: "05/09/2020", dayCounter: 6 },
  { id: "1", billStatus: "Non-Billable", tDate: "05/09/2020", dayCounter: 0 },
  { id: "1", billStatus: "Billable", tDate: "05/09/2020", dayCounter: 0 },
  { id: "1", billStatus: "Billable", tDate: "05/09/2020", dayCounter: 0 },
  { id: "3", billStatus: "Billable", tDate: "05/10/2020", dayCounter: 0 },
  { id: "2", billStatus: "Billable", tDate: "05/10/2020", dayCounter: 0 },
  { id: "2", billStatus: "Non-Billable", tDate: "05/10/2020", dayCounter: 0 },
];

const billableDates = data.reduce((res, obj) => {
  if (!res[obj.id]) {
    if (obj.billStatus === "Billable") {
      res[obj.id] = { dates: new Set() };
      res[obj.id].dates.add(obj.tDate);
    }
  } else {
    res[obj.id].dates.add(obj.tDate);
  }
  return res;
}, {});

const modifiedData = data.reduce((store, obj) => {
  if (!store[obj.id] && obj.billStatus === "Billable") {
    store[obj.id] = obj;
    store[obj.id].dayCounter = billableDates[obj.id].dates.size;
    store[obj.id].dates = [...billableDates[obj.id].dates];
  }
  return store;
}, {});

console.log(Object.values(modifiedData));

这篇关于如何创建日期计数器?我尝试使用减少功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆