Home > Software engineering >  I need to get the total amount by date and type of operation
I need to get the total amount by date and type of operation

Time:12-05

I currently have and array of objects with these parameters:

const data = [

{
amount: 700,
​​
date: "01/12/2022",
​​
description: "Test expense 3",
​​
id: "7qlck5WmRrHmpHcek3fb",
​​
time: "15:47:01",
​​
type: "expense",
},

amount: 150,
​​
date: "03/12/2022",
​​
description: "Test income 3",
​​
id: "7qlck5WmRrHmpHcek3fb",
​​
time: "15:47:01",
​​
type: "income",
]

This is an expense app that I am working on and I want to display some information in a chart.

I have tried this function:

  const result = Object.values(
    data.reduce((acc, { type, amount, date }) => {
      if (!acc[date]) acc[date] = Object.assign({}, { type, amount, date });
      else acc[date].amount  = amount;
      console.log(acc[date].type);
      return acc;
    }, {})
  );

But it's not returning exactly what I want and I am not sure what I need to change. Right now, this is what it returns:

Object { type: "income", amount: 850, date: "01/12/2022" }
​
1: Object { type: "income", amount: 550, date: "03/12/2022" }
​
2: Object { type: "expense", amount: 150, date: "04/12/2022" }
​
length: 3

It does create new objects based on the date, but on top of that, I need to get the total amounts for expenses and income separately based on the type of operation.

For eg:

const returnedData = [
{type: 'income', amount: (sum of all incomes on that given date), date: date,
type: 'expense', amount: (sum of all expenses on that given date), date: date

Thanks all!

CodePudding user response:

To return the information in the format you want, you will need to make a few changes to your code. First, you can use the reduce() method to group the data by date and then calculate the total amount for each type of operation (income or expense) for each date. Then, you can use the map() method to transform the resulting object into an array of objects in the format you want.

Here's an example of how you could do this:

const data = [
  {
    amount: 700,
    date: "01/12/2022",
    description: "Test expense 3",
    id: "7qlck5WmRrHmpHcek3fb",
    time: "15:47:01",
    type: "expense",
  },
  {
    amount: 150,
    date: "03/12/2022",
    description: "Test income 3",
    id: "7qlck5WmRrHmpHcek3fb",
    time: "15:47:01",
    type: "income",
  }
];

const result = Object.values(
  data.reduce((acc, { type, amount, date }) => {
    // Check if the date already exists in the accumulator object
    if (!acc[date]) {
      // If not, create a new object for the date and initialize the
      // amount for each type of operation to 0
      acc[date] = {
        date,
        income: 0,
        expense: 0,
      };
    }

    // Add the amount to the appropriate property in the date object
    acc[date][type]  = amount;

    return acc;
  }, {})
);

// Transform the resulting object into an array of objects
const returnedData = result.map(({ date, income, expense }) => [
  { type: 'income', amount: income, date },
  { type: 'expense', amount: expense, date },
]);

console.log(returnedData);

This code will group the data by date and calculate the total income and expense for each date. It will then return an array of objects in the format you want, with separate objects for income and expense for each date.

The output will look like this:

[
  [
    { type: 'income', amount: 0, date: '01/12/2022' },
    { type: 'expense', amount: 700, date: '01/12/2022' }
  ],
  [
    { type: 'income', amount: 150, date: '03/12/2022' },
    { type: 'expense', amount: 0, date: '03/12/2022' }
  ]
]
  • Related