javaScript Sum Array Object Values – Examples

javaScript Sum Array Object Values – Examples

In This javaScript Sum Array Object Values Tutorial. We will learn, how to sum of array values, sum array of objects reduce, javascript reduce array of objects by property, javascript reduce array of objects by key, sum of array values using javascript for loop, javascript map array of objects.

Here will take e.g. sum of array values using for loop, sum of array object values using javaScript reduce() method, javascript reduce array of objects by key, javascript reduce array values using javascript map() method.

javaScript Sum Array Object Values

Follow the below examples of sum array object values with several techniques and javascript methods:

First Method – javascript sum array values

Now we will take the first example of sum array values using the javascript for a loop.

var numArr = [10, 20, 30, 40] // sums to value = 100
var sum = 0;
for (var i = 0; i < numArr.length; i++) {
  sum += numArr[i]
}
document.write( "javascript- Sum of the array value is :- " + sum ); 

Ex:-

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>javascript sum array values</title>
</head>
<body>
  <script type = "text/javascript">
    var numArr = [10, 20, 30, 40] // sums to value = 100
    var sum = 0;
    for (var i = 0; i < numArr.length; i++) {
      sum += numArr[i]
    }
    document.write( "javascript- Sum of the array value is :- " + sum ); 

  </script>  
</body>
</html>

Result of the above code is :- 100

Second Method – javaScript sum array values using reduce() method

Next, we will use the reduce method of javascript for the sum of array values in javascript. You can see the example the below:

    var numArr = [10, 20, 30, 40] // sums to value = 100
    var sum = numArr.reduce(function(a, b){return a+b;})
    document.write( "javascript- Sum of the array value is :- " + sum ); 

Ex:-

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>javascript sum array values using reduce() method</title>
</head>
<body>
  <script type = "text/javascript">
    var numArr = [10, 20, 30, 40, 50] // sums to value = 100
    var sum = numArr.reduce(function(a, b){return a+b;})
    document.write( "javascript- Sum of the array value is :- " + sum ); 
  </script>  
</body>
</html>

Result of the above code is: 150

Third Method – javascript sum array values using map() method

We will also use the javascript map method to the sum of array values. If you want the sum of the array values, you can use like below:

   var numArr = [10, 20, 30, 40] // sums to value = 100
   var sum = 0;
   numArr.map(function(x){sum+=x})
   document.write( "javascript- Sum of the array value is :- " + sum ); 

Ex:-

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>javascript sum array values using map() method</title>
</head>
<body>
  <script type = "text/javascript">
    var numArr = [10, 20, 30, 40, 5] // sums to value = 100
    var sum = 0;
    numArr.map(function(x){sum+=x})
    document.write( "javascript- Sum of the array value is :- " + sum ); 
  </script>  
</body>
</html>

Result of the above code is:- 105

Fourth Method – javascript sum array of objects

Next, we will take an example of a sum array of objects using the javascript for loop method.

    var numArr = [
        {  name: 'a', num: 50},
        {  name: 'b', num: 50},
        {  name: 'c', num: 75},
        {  name: 'd', num: 35},
        {  name: 'e', num: 25 },
    ];

    var sum = 0;
    for (var i = 0; i < numArr.length; i++) {
      sum += numArr[i].num
    }
    document.write( "javascript- Sum of the array value is :- " + sum ); 

Ex:-

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>javascript sum array of objects</title>
</head>
<body>
  <script type = "text/javascript">
    var numArr = [
        {  name: 'a', num: 50},
        {  name: 'b', num: 50},
        {  name: 'c', num: 75},
        {  name: 'd', num: 35},
        {  name: 'e', num: 25 },
    ];

    var sum = 0;
    for (var i = 0; i < numArr.length; i++) {
      sum += numArr[i].num
    }
    document.write( "javascript- Sum of the array value is :- " + sum ); 

  </script>  
</body>
</html>

Result of the above code is:- 235

Five Method – javascript sum array of objects reduce

Here, we will take an example of the sum array of objects using the javascript reduce method.

    var numArr = [
        {  name: 'a', num: 50},
        {  name: 'b', num: 50},
        {  name: 'c', num: 75},
        {  name: 'd', num: 35},
        {  name: 'e', num: 25 },
        {  name: 'f', num: 40 },
    ];

    var sum = numArr.reduce(function (total, currentValue) {
        return total + currentValue.num;
    }, 0);

    document.write( "javascript- Sum of the array value is :- " + sum ); 

Ex:-

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>javascript sum array of objects</title>
</head>
<body>
  <script type = "text/javascript">
    var numArr = [
        {  name: 'a', num: 50},
        {  name: 'b', num: 50},
        {  name: 'c', num: 75},
        {  name: 'd', num: 35},
        {  name: 'e', num: 25 },
        {  name: 'f', num: 40 },
    ];

    var sum = numArr.reduce(function (total, currentValue) {
        return total + currentValue.num;
    }, 0);

    document.write( "javascript- Sum of the array value is :- " + sum ); 

  </script>  
</body>
</html>

Result of the above code is:- 275

You may like

  1. Convert String to Array JavaScript
  2. JavaScript Array splice() Method By Example
  3. Sorting Using JavaScript Array Sort() Method
  4. JavaScript: Clone an Array & Object By Examples
  5. Remove (First, last, specific) Element From Array Javascript
  6. javaScript Push() Array By Example
  7. javaScript Push Array, Items Into Array Example
  8. Convert Array to Comma-Separated String javaScript
  9. JavaScript Check the Object | isArray() Function
  10. JavaScript: Important Array Methods

Leave a Reply

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