1

I have data in which there are arrays. in the array, there is a value which is claimed_value. I need the sum of all claimed_value of arrays. an example I have array all have the claimed_value. but I need to sum of all the claimed_value which have the status=settled in the array.

this is how I'm calling the API.

getClaims(){
 if(this.userFilter.company_id){
 let url = 
  'http://api.igiinsurance.com.pk:8888/insurance_IGItakaful/insurance- 
  api/get_claims.php?company_id='+this.userFilter.company_id;
 }else{
 let url = 
 'http://api.igiinsurance.com.pk:8888/insurance_IGItakaful/insurance- 
 api/get_claims.php?offset=0&limit=200';
}
  this.clientData = this.httpClient.get(url).
  subscribe(data => {
  console.log(data);
  this.data = data.records;
  var status = 'settled';
  var status2 = 'submitted';


 var countsettled = this.data.filter((obj) => obj.status === 
 status).length;
 var countunsettled = this.data.filter((obj) => obj.status === 
 status2).length;


console.log(countsettled);
this.countsettled = countsettled;
console.log(countunsettled);
this.countunsettled = countunsettled;

  });

 }

enter image description here

2
  • What you tried? Commented Apr 29, 2019 at 13:02
  • as @CharybdeBE answer this is the sum of all claimed_value in array . but i want to add only array value which gave status = settled . Commented Apr 29, 2019 at 13:07

2 Answers 2

4

Your question is more general javascriptthan angular

You can sum element of array by using the Array.reduce function

You can sum on a specific status by filtering first

Ie

const sum = this.data.filter(item => item.status === 'settled')
                     .reduce((acc, item) => acc + Number(item.claimed_value), 0);

Note that as said in comment you received a string and not a numberso you must convert it at first (see the Number() function in thereduceclosure)) otherwise the+` operator will just concatenate the string

Sign up to request clarification or add additional context in comments.

3 Comments

thanks i have updated my question i need the sum in which the status value is settled .
thanks but its showing wrong value its not sum the values
@UmaizKhan I see that in the data you have claimed_value is a string not a number so the + operator is concataining string, this can be fixed by transforming string to number (cf updat eof my post))
0
let finalSum = 0;
this.data.forEach((item) => {
    if(item.status === 'settled'){
        finalSum += item.claimed_value
    }
}); 
console.log("final sum is ", finalSum);

Hello, This will loop on all the arrays in your data list , and add the number of claimed_value to the finalSum . Hope this will work for you.

8 Comments

what is the value of sum ? i need to console the value of sum of values
the value of the sum here will be the finalSum , if you add console.log(finalSum) after the if statement , it will show the sum of all the arrays that has status = settled
its now showing the value its showing all the claimed_value which have status of settled . its not suming the values just showing all the array value which status is settled
I edited my answer, place the console after the loop and not after the if . just like my answer
yes its showing the value now . but the value of sum is showing wrong .
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.