What is a method that can be used to increment letters?

前端 未结 14 2823
我在风中等你
我在风中等你 2020-11-27 04:19

Does anyone know of a Javascript library (e.g. underscore, jQuery, MooTools, etc.) that offers a method of incrementing a letter?

I would like to be able to do somet

14条回答
  •  广开言路
    2020-11-27 04:29

    I needed to use sequences of letters multiple times and so I made this function based on this SO question. I hope this can help others.

    function charLoop(from, to, callback)
    {
        var i = from.charCodeAt(0);
        var to = to.charCodeAt(0);
        for(;i<=to;i++) callback(String.fromCharCode(i));
    }
    
    • from - start letter
    • to - last letter
    • callback(letter) - function to execute for each letter in the sequence

    How to use it:

    charLoop("A", "K", function(char) {
        //char is one letter of the sequence
    });
    

    See this working demo

提交回复
热议问题