How to loop through the alphabet via underscoreJS
var alphabet = "abcdefghijklmnopqrstuvwxyz".split("");
_.each(alphabet, function(letter) {
console.log(letter);
});
That's how you could do it.
Create a range with the charcodes
var alphas = _.range( 'a'.charCodeAt(0), 'z'.charCodeAt(0)+1 ); // [97 .. 122]
Create an array with the letters
var letters = _.map(alphas, a => String.fromCharCode(a)); // see @deefour comment // Non ES6 version // var letters = _.map(alphas, function(a) { // return String.fromCharCode(a); // }); // [a .. z]
Inject into your template
var tpl = '<ul>'+ '<% _.each(letters, function(letter) { %>'+ '<li><%= letter %></li>'+ '<% }); %>'+ '</ul>'; var compiled = _.template(tpl); var html = compiled({letters : letters});
And a demo http://jsfiddle.net/hPdSQ/17/
var alphas = _.range(
'a'.charCodeAt(0),
'z'.charCodeAt(0)+1
);
var letters = _.map(alphas, a => String.fromCharCode(a));
var tpl =
'<ul>'+
'<% _.each(letters, function(letter) { %>'+
'<li><%= letter %></li>'+
'<% }); %>'+
'</ul>';
var compiled = _.template(tpl);
var html = compiled({letters : letters});
document.getElementById('res').innerHTML = html;
<script src="http://underscorejs.org/underscore-min.js"></script>
<div id='res'></div>