How to generate an array of alphabet in jQuery?

前端 未结 15 2200
被撕碎了的回忆
被撕碎了的回忆 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:23

    const ALPHA = Array.from({ length: 26 }, (_, i) => String.fromCharCode('a'.charCodeAt(0) + i)); // ['a', 'b', ...'z']
    

    I believe the above code is more idiomatic. Short enough to be an inline code. You don't have to remember the charCode of your start letter and configurable to retrieve subsets of the alphabet by simply controlling the length and start letter e.g

    Array.from({ length: 3 }, (_, i) => String.fromCharCode('x'.charCodeAt(0) + i)) // ['x', 'y', 'z]
    

提交回复
热议问题