How to sum data in array (Fetch JSON array to IONIC view)

I am facing an issue. In the JSON array given below:

$results = [
{ 
    proj_name: rental,
    act_name: income,
    amount: "1000"
},
{ 
    proj_name: loan,
    act_name: income,
    amount: "7000"
},
{
    proj_name: rental,
    act_name: initiall,
    amount: "3000"
},
{
    proj_name: loan,
    act_name: expend,
    amount: "-11000"
},
{
    proj_name: rental,
    act_name: income,
    amount: "4000"
},
{
    proj_name: rental,
    act_name: expend,
    amount: "-5000"
},
{
    proj_name: rental,
    act_name: initial,
    amount:"6000"
},
{
    proj_name: loan,
    act_name: expend,
    amount: "-8000"
},
{
    proj_name: loan,
    act_name: initial,
    amount: "9000"
},
{
    proj_name: rental,
    act_name: expend,
    amount: "-2000"
},
{
    proj_name: loan,
    act_name: income,
    amount: "10000"
},
{
    proj_name: loan,
    act_name: initial,
    amount:"12000"
}
];

We can see here we have two proj_name as rental and loan.

Also transactions are categorized further with in activities as income, expend and initial.

My question is how I can loop through $results array to calculate sum for each transaction type that I listed above. I would like the output to be like this:

|------------------|
|      rental      |
|------------------|
|income : 5000     |
|expend : -7000    |
|initial : 9000    |
|------------------|
|Sum Rental: 7000  |
|__________________|

|------------------|
|      loan        |
|------------------|
|income : 17000    |
|expend : -19000   |
|initial : 21000   |
|------------------|
|Sum Loan: 19000   |
|__________________|

And I want to create new array as in this form:

$results = [
  -{
        projname: rental,
        income: 5000,
        expend : -7000,
        initial : 9000,
        sum : 7000
    },
  -{
        projname: loan,
        income: 17000,
        expend : -19000,
        initial : 21000,
        sum : 19000
    }
];

just use for loop where you get the data from JSON

for (var index = 0; index < this.data.length; index++) {
       var element = this.services[index];
       if (element.proj_name== "loan") {
           this.sumLoan=this.sumLoan+ element.amount;    
       }
}
1 Like

Thanks for your kindly reply, but as on the $results has many proj_name how would we do?

you can use else if condition. Simple !!!:smile::smile:

if (element.proj_name== "loan") {
           this.sumLoan=this.sumLoan+ element.amount;    
}
else  if (element.proj_name== "rental") {
           this.sumrental=this.sumrental+ element.amount;    
}

Hi

Consider using Array.map and Array.filter, instead of for-loop

So this.results.map(item=>{ do something})

Less coding. Possibly faster execution. And taking you closer to Observable heaven, which you bump into working with http.

Tom

how to perform sumation in filter ?
can you give example according to above result ?

Hi

This example is not conplex enough to be using filter as map would suffice

I wouldnt use the if then else construct but store the amount in an ovject where the proj_name would be a property

So this.sumResults[proj_name] += amount

And of course having sumResult[proj_name] assigned amount if it was null before

Regards
Tom

1 Like

may be it is better way because i am not used with filter yet :sweat_smile::sweat_smile:

I meant if there’s more proj_name out of these two how should we do? What you did is just fix for two proj_name such loan and rental

Just make it clear, according to JSON given one proj_name can have many act_name that can be able to add in. So, imagine that one porjec_name (and it many proj_name) has many act_name

So, How to loop sum of amount in each project that have carry may activities ?

Regards,

Akephasith.

My solutions solves this for now and the future content of proj_name providig it adheres to proper naming convention of keys in objects.

Plain vanilla javascript basics

Regards

Tom

Can you provide us how to use your solution base on the variable given? (to avoid confusing or mistake) Thank you.

Best regard,

Akephasith