Warning: session_start(): open(/tmp/sess_9909afaa92cff2cd0d5fd741e4850abf, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Sum The Values Of An Object Using JavaScript - LearnShareIT

How To Sum The Values Of An Object Using JavaScript

Sum the Values of an Object using JavaScript

Now you will discover how to sum the values ​​of an object using JavaScript with two simple methods. This is one of the most important skills when you work with an object’s values in JavaScript.

Sum the values of an object using JavaScript

Using for loop with Object.values()

This Object.values() function returns an array of the values in an object.

Syntax:

Object.values(object)

Parameter:

  • object: the object you want to sum the values

You can use the Object.values() method and pass your object to its parameter. After that you use a for loop to iterate through the array returned by this function. 

let object = {learn:2000,share:20,it:2};
let sum = 0;

// iterate through an arrray of the object's values
for (value of Object.values(object))
    sum = sum + value;

console.log(sum);

Output: 

2022

The above example uses an object in which each value is a number. So the sum is actually adding numbers together and the result is 2000+20+2 = 2022. If the values in your object are all strings then it will return the concatenated string. However, if some are strings and some are numbers, for example:

let object = {learn:2000,share:20,it:'2',one : 1, two: 2};
let sum = 0;

// iterate through an arrray of the object's values
for (value of Object.values(object))
    sum = sum + value;

console.log(sum);

Output: 

2020212

If your values have different types, such as numbers and strings, then the result returned is a string. As 2000 and 20 are numbers then the sum will still be numbers. Then when it reaches the value ‘2’, which is a string, then it will convert the sum variable into a string and then concatenate it with the string. From now on, the sum variable will be a string so any numbers or values added to it will be concatenated as a string also.

Using reduce() with Object.values()

The reduce() method executes the user-supplied callback function “reducer” on each element of the array in the specified order, passing the computed return value of the previous element. The end result of running the reducer on all elements of the array is a single value.

Syntax:

reduce((previousValue, currentValue) => { /* … */ } , initialValue)

Parameters:

  • previousValue: The value resulted as the previous reduce call.
  • currentValue: The value of the current element. The first reduce call will have the value of the first element in the array.
  • initialValue: A value to which previousValue is initialized the first time the callback is called.

Example:

let object = {learn:2000,share:20,it:'2',one : 1,two: 2};

// execute a function which accumulates the object's value to the sum variable.
let sum = Object.values(object).reduce((sum,value) => sum+value, 0);

console.log(sum);

Output: 

2020212

Anthor example:

let object = {learn:2000,share:20,it:2};

// execute a function which accumulates the object's value to the sum variable.
let sum = Object.values(object).reduce((sum,value) => sum+value, 0);

console.log(sum);

Output: 

2022

The logic behind this method is very simple. Our accumulator sum variable, initialized 0, is summing all iterated values of the object. However, some programmers may find the reduce() function is hard to acknowledge because it is relative to functional programming concepts. Such functions will use the best-optimized algorithm to reduce running complexity and hence we recommend you use this way.

Summary

We have learned two ways to sum the values of an object using JavaScript. With the help of our instructions in this tutorial and others, you can finally succeed in achieving the task.

Maybe you are interested:

Leave a Reply

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