Sum all properties in object with reduce method - Javascript Object

Javascript examples for Object:Object Property

Description

Sum all properties in object with reduce method

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){//from w  w w .ja  v a 2 s  . c o m
var obj = {"speed":299,"equipment":49,"test":49,"additional":50,"optional":"299"};
var sum = Object.keys(obj).reduce(function(prev, current, index) {
    return prev + (+obj[current]);
}, 0);
console.log(sum)
    }

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

Related Tutorials