Is there a way to make an array of months and their Income from JSON?

Viewed 240

I'm working on an Accounting System. I'm trying to display a bar chart with information about the Incomes and Expenses of every month. I get that information from a database via AJAX and I get the next JSON:

{
   "results":{
      "historic":[
         {
            "fecha":"2020-07-15",
            "balance":"5433"
         },
         {
            "fecha":"2020-07-15",
            "balance":"6883.25"
         },
         {
            "fecha":"2020-07-15",
            "balance":"5383.15"
         },
         {
            "fecha":"2020-07-18",
            "balance":"12383.15"
         },
         {
            "fecha":"2020-08-20",
            "balance":"13385.35"
         }
      ],
      "monthly":{
         "income":[
            {
               "month":"July\n",
               "July\n":"5433"
            },
            {
               "month":"July\n",
               "July\n":"1450.25"
            },
            {
               "month":"July\n",
               "July\n":"0"
            },
            {
               "month":"July\n",
               "July\n":"7000.00"
            },
            {
               "month":"August\n",
               "August\n":"1002.20"
            }
         ],
         "expense":[
            {
               "month":"July\n",
               "July\n":"0"
            },
            {
               "month":"July\n",
               "July\n":"0"
            },
            {
               "month":"July\n",
               "July\n":"1500.10"
            },
            {
               "month":"July\n",
               "July\n":"0"
            },
            {
               "month":"August\n",
               "August\n":"0"
            }
         ]
      }
   }
}

The part under Historic is used for another chart that already works but the part under Monthly contains all the information that I need for the chart I'm currently working on.

THIS IS WHAT I NEED TO DO:
I need to create an array using Javascript that can contain the name of the month and the total Income or Expense of it. The problem is that I get one entry for every transaction on that month and I need to sum up. It's kinda difficult to explain it so I'm gonna give you an example.

I get this:

  • July : $1000
  • July : $300
  • July : $50
  • August : $400
  • August : $7000

And I need to convert it to something like this inside of an array:

  • July : $1350
  • August : $7400

This is what I been working on but I think it is not the way to go:

//DRAW THE MONTHLY RESULTS GRAPHIC

      var logs = [];

            for(var i = 0; i < jsonObj['results']['historic'].length; i++) {
                var results = jsonObj['results']['monthly'];
                if(logs.some(logs => logs.month === results['income'][i].month)){
                     console.log("Object found inside the array.");
                     //Need a way to get the total of the month from the logs Array.
                     var newTotal = results['income'][i].month + oldTotal;
                     logs.month = newTotal;
                 } else{
                     console.log("Object not found.");
                     logs.push(results['income'][i]);
                 }
          

             
            }

            console.log(logs);

Thanks to everyone. I'm very new at this forum stuff and I hope I managed to explain it well.

1 Answers

You can do the following, where the overall idea is to generate a new object where you store your values in. You loop over ["income", "expense"] and inside this loop, you then loop over all transactions. If a transaction from that month has not already been added to your now object, we set the amount already added to 0 and otherwise what it actually is. We can then add the new amount to the current amount.

You already got this idea somewhat yourself, but using an array makes lookup harder than necessary.

function sum_up_data(raw_data) {
  monthly = {
    income: {},
    expense: {}
  };

  Object.keys(monthly).forEach(transaction_type => {
    raw_data.results.monthly[transaction_type].forEach(raw_transaction => {
      const month = raw_transaction.month;
      const amount_to_add = parseFloat(raw_transaction[month]);
      const current_amount = monthly[transaction_type][month] || 0;
      monthly[transaction_type][month] = current_amount + amount_to_add;
    });
  });

  return { results: { monthly } };
}


// Test that it works
raw_data = {
  results: {
    historic: [], // Omitted 'historic' values for brevity
    monthly: {
      income: [
        {
          month: "July\n",
          "July\n": "1450.25",
        },
        {
          month: "July\n",
          "July\n": "0",
        },
        {
          month: "August\n",
          "August\n": "1002.20",
        },
      ],
      expense: [
        {
          month: "July\n",
          "July\n": "0",
        },
        {
          month: "July\n",
          "July\n": "1500.10",
        },
        {
          month: "August\n",
          "August\n": "0",
        },
      ],
    },
  },
};

console.log(raw_data);
console.log(sum_up_data(raw_data).results.monthly);

Related