Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I need to get the total amount by date and type of operation

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  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!

>Solution :

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' }
  ]
]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading