Search code examples
javascriptdatesum

How to sum data by date filter in Javascript?


I try to find a solution to sum cost data (to group thme by day) with ISO date information in object.

My data object looks like :

0: {id: 45, title: 'abo', cost: 10, recurrent: true, date: '2022-02-09T20:43:30.139Z'}
1: {id: 123, title: 'test 4', cost: 20, recurrent: false, date: '2022-02-09T20:43:30.139Z'}
2: {id: 125, title: 'test 3', cost: 50, recurrent: false, date: '2022-02-06T23:00:00.000Z'}
3: {id: 125, title: 'test 2', cost: 100, recurrent: false, date: '2022-02-05T10:00:00.000Z'}
4: {id: 125, title: 'test 1', cost: 122, recurrent: false, date: '2022-02-04T23:00:00.000Z'}

The expected result : sumData = [222,50,30]

  • Total cost for the 2022-02-05 = 222
  • Total cost for the 2022-02-06 = 50
  • Total cost for the 2022-02-09 = 30

I try to sum with something like that :

    let currentDate     = '';
    let currentTotal    = 0;
    let sumData         = [];

    if(data) {
        for (let index = 0; index < data.length; index++) {
             
            //init date to compare with
            if(currentDate === '') {
                currentDate = new Date(data[index].date);
                currentTotal = data[index].cost;
            }
            else {
                const nextDate =  new Date(data[index].date);
                //test if is same day, sum expenses and add in sumData array
                if(currentDate.getFullYear() === nextDate.getFullYear() &&
                currentDate.getMonth() === nextDate.getMonth() &&
                currentDate.getDate() === nextDate.getDate()) 
                {
                    console.log('DATE = ' + currentDate.getDate());
                    console.log(data[index].date);
                    console.log('COST = ' + data[index].cost);
                    console.log('currentTotal = ' + currentTotal);
                    currentTotal = currentTotal + data[index].cost;
                    console.log(index + ' - COST = ' + data[index].cost);
                    console.log('TOTAL = ' + currentTotal);
                    console.log('==========='); 
                }
                else {
                    //push last valid sum before reset
                    currentTotal = 0;
                    //reset sum with last data
                    currentTotal = data[index].cost;
                }
                currentDate =  new Date(data[index].date);
            }   
        }
    }
    console.log('== SUM data ==');
    sumData.push(currentTotal);


Solution

  • You can try to use .reduce() and Object.values() to achieve your goal:

    const data = [
    {id: 45, title: 'abo', cost: 10, recurrent: true, date: '2022-02-09T20:43:30.139Z'},
    {id: 123, title: 'test 4', cost: 20, recurrent: false, date: '2022-02-09T20:43:30.139Z'},
    {id: 125, title: 'test 3', cost: 50, recurrent: false, date: '2022-02-06T23:00:00.000Z'},
    {id: 125, title: 'test 2', cost: 100, recurrent: false, date: '2022-02-05T10:00:00.000Z'},
    {id: 125, title: 'test 1', cost: 122, recurrent: false, date: '2022-02-04T23:00:00.000Z'}
    ]
    
    const res = Object.values(data.reduce((resMap, obj) => {
      const date = new Date(obj.date).toLocaleString().split(',')[0]
      if (resMap[date] !== undefined)
        resMap[date] += obj.cost
      else
        resMap[date] = obj.cost
    
      return resMap;
    }, {}))
    
    console.log(res)

    Update: use ISO8601 date