Lodash.js – countBy() function
A simple Lodash.js example of the countBy() function, which creates an array of key value pairs, describing how many items with specific values are present in the product of the function after invoking the anonymous function.
<!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 = [12.7, 12.2, 12.1, 3, 3, 8.1, 8.125];
firstDiv.innerHTML = "Having the array: " + firstValue + " we will create an array of key value pairs describing how many items with specific values are present in the product of the function after invoking the anonymous function...";
var result = _.countBy(firstValue, function(c) {
return Math.ceil(c);
});
secondDiv.innerHTML = "...so we get: " + result;
Object.keys(result).forEach(function (key) {
document.write("<br>After Math.ceil we have the value: " + key + " showing up " + result[key] + " times." );
});
</script>
</body>
</html>