How to loop through the alphabet via underscoreJS

后端 未结 9 1584
情话喂你
情话喂你 2021-01-04 01:05

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.

As a

相关标签:
9条回答
  • 2021-01-04 01:52

    for (var i = 'a'.charCodeAt(0); i <= 'z'.charCodeAt(0); i++) {
      console.log(String.fromCharCode(i));
    }

    0 讨论(0)
  • 2021-01-04 01:53
    for(var letter=65;letter<91;letter++)
    {
    var _char = String.fromCharCode(letter);
    console.log(_char);
    }
    

    or use from 97 - 123 ascii code for lowercase letters

    0 讨论(0)
  • 2021-01-04 01:55

    Here's an improved* version of @Medo Medo's pure JS code:

        var letters=[], letter_first = 'a', letter_last = 'z' // you can also use A and Z
        for (var letter=letter_first.charCodeAt(0);letter<=letter_last.charCodeAt(0);letter++)
          letters.push(String.fromCharCode(letter))
        document.write(letters.join(''))

    • Fixed the "var" declaration
    • Added direct letter detection
    • Collected the result into an array in order to have only one output
    • Made the code runnable right here
    0 讨论(0)
提交回复
热议问题