如何通过underscoreJS循环字母表 [英] How to loop through the alphabet via underscoreJS

查看:27
本文介绍了如何通过underscoreJS循环字母表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 BackboneJS 视图中使用 Underscore 的 template() 方法.我想在视图中显示字母列表,以便按字母对集合进行排序.

I'm using Underscore's template() method in BackboneJS views. I'd like to show a list of alphabet letters in my view in order to sort a collection by letter.

因此,在我看来,我有一个包含 26 个链接的列表(一个链接 = 一个字母).我没有复制粘贴每个链接(这对代码可维护性非常不利),我想知道是否可以通过 underscoreJS 遍历字母表.

As a result, I have a list of 26 links (one link = one letter) in my view. Instead of copy-pasting each link (which is very bad for code maintainability), I was wondering if it was possible to loop through the alphabet via underscoreJS.

显示结果:

<li ><a href="#">a</a></li>
<li ><a href="#">b</a></li>
<li ><a href="#">c</a></li>
...
<li ><a href="#">z</a></li>

推荐答案

var alphabet = "abcdefghijklmnopqrstuvwxyz".split("");
_.each(alphabet, function(letter) {
  console.log(letter);
});

这就是你可以做到的.

这篇关于如何通过underscoreJS循环字母表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆