Lodash.js – groupBy() function

A simple Lodash.js example of the groupBy() function, which groups the initial values under the resulting keys.
<!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 Angular.js 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 = [3.2,2.4,2.6,2.8];

  firstDiv.innerHTML = "Having the object: " + firstValue + " will group the initial values under the resulting keys...";

    var result = _.groupBy(firstValue, function(num) {

      return Math.ceil(num);

    });

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

  Object.keys(result).forEach(function (key) {
      document.write("<br>key from result: " + key + ", values which pass the condition: " + result[key]);
  });

</script>
</body>

</html>

Responses

0 Replies