Angular Sum all properties value in JSON object

Angular Sum all properties value in JSON object

Today, We want to share with you Angular Sum all properties value in JSON object.
In this post we will show you Sum all properties value in object Using Angularjs Example, hear for Iterating over arrays and objects in angular js we will give you demo and example for implement.
In this post, we will learn about How To Create, Modify, and Loop through Objects in angular with an example.

The reduce() method/function reduces function the array to a single value. The reduce()/function method executes a provided function for each value of the array (from left-to-right execute). simple to easy way The return value of the function or method is stored in an all the items value accumulator (all the data result/total).(Sum each value of an array) Note: reduce() function does not execute the function for array elements without values.

READ :  AngularJS Cookies Set Get and Remove Cookies

Example 1 : sum a property value in an array Using Angularjs Example

json Data

$scope.sumtotal2 =[[1,1],[2,2],[3,3],[4,4],[6,5],[6,6],[5,7],[5,8],[5,9]]; 

Script Data :

calculating sum of repeated elements in angularjs ng-repeat

 
$scope.sum1= $scope.sumtotal2.reduce(function(sum, pair) 
{
	return sum + pair[0];
}, 0);

sum a property value in an array Using Angularjs Example

$scope.items = [
    {  infinityknowName: 'laravel', UnitPrice: 100},
    {  infinityknowName: 'vuejs', UnitPrice: 550},
    {  infinityknowName: 'php', UnitPrice: 745},
    {  infinityknowName: 'Angularjs', UnitPrice: 315},
    {  infinityknowName: 'HTML', UnitPrice: 205 },
];

Array.prototype.sum = function (prop) {
    var total = 0
    for ( var i = 0, _len = this.length; i < _len; i++ ) {
        total += this[i][prop]
    }
    return total
}

console.log($scope.items.sum("UnitPrice"))

Calculating sum of repeated elements in AngularJS ng-repeat

Total: {{ get_allTotal() }}

Script – Controller

<script>
$scope.get_allTotal = function(){
    var total = 0;
    for(var i = 0; i < $scope.student.uniq.length; i++){
        var uniq = $scope.cart.uniq[i];
        total += (uniq.price * uniq.quantity);
    }
    return total;
}
</script>

Perform Group By and Sum by property in angularjs

<!doctype html>
<html xmlns:ng="http://angularjs.org">

 <script type="text/javascript" ng:autobind src="angular-0.9.9.min.js"></script>
 <body>
	<h1>angular.Array.sum -infinityknow.com</h1>
  <table ng:init="billData= {items:[{product_qty:10, description:'gadget', productcost:9.95}]}">
    <tr><th>product_qty</th><th>Description</th><th>productcost</th><th>Total</th><th></th></tr>
    <tr ng:repeat="item in billData.items">
      <td><input name="item.product_qty" value="1" size="4" ng:required ng:validate="integer"></td>
     <td><input name="item.description"></td>
       <td><input name="item.productcost" value="0.00" ng:required ng:validate="number" size="6"></td>
      <td>{{item.product_qty * item.productcost | currency}}</td>
      <td>[<a href ng:click="billData.items.$remove(item)">X</a>]</td>
    </tr>
    <tr>
      <td><a href ng:click="billData.items.$add()">add item</a></td>
      <td></td>
      <td>Total:</td>
      <td>{{billData.items.$sum('product_qty*productcost') | currency}}</td>
    </tr>
  </table>
 </body>
</html>

Example 1

READ :  Angular Interval Service Example

Example 2

Add a Comment

Your email address will not be published. Required fields are marked *