How to generate an array of alphabet in jQuery?

前端 未结 15 2259
被撕碎了的回忆
被撕碎了的回忆 2020-12-07 15:57

In Ruby I can do (\'a\'..\'z\').to_a and to get [\'a\', \'b\', \'c\', \'d\', ... \'z\'].

Do jQuery or Javascript provide a similar construc

15条回答
  •  难免孤独
    2020-12-07 16:12

    Just for fun, then you can define a getter on the Array prototype:

    Object.defineProperty(Array.prototype, 'to_a', {
      get: function () {
        const start = this[0].charCodeAt(0);
        const end = this[1].charCodeAt(0);
        return Array.from(Array(end - start + 1).keys()).map(n => String.fromCharCode(start + n));
      }
    });
    

    Which makes it possible to do something like:

    ['a', 'z'].to_a; // [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", ..., "z" ]
    

提交回复
热议问题