Lodash.js – sum() function

A simple Lodash.js example of the sum() function, you can sum any values with it. Even key values from inside of objects.
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <meta name="description" content="Tizen example"/>
    <title>Tizen lodash.js example</title>
    <script src="js/lodash.min.js"></script>
</head>
<body>
<div id="first">
</div>
<br>
<div id="second">
</div>

<script>

    var firstDiv = document.getElementById('first');
    var secondDiv = document.getElementById('second');

    var firstValue = {'grapes':20, 'carrots':12, 'apples':14, 'cucumbers':20, 'oranges':90};

  firstDiv.innerHTML = "Having the object and key values in the " + firstValue + " {'grapes':20, 'carrots':12, 'apples':14, 'cucumbers':20, 'oranges':90}, we will sum up all of the key values...";

    var result = _.sum(firstValue);

  secondDiv.innerHTML = "...and we will get: " + result;

</script>
</body>

</html>

Responses

0 Replies