How to generate an array of alphabet in jQuery?

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

    Using JavaScript's Array.from syntax allows you to create an array and perform a mapping function on each of the array elements. Create a new array of length 26 and on each element set the value equal to the string obtained from the char code of the index of the current element plus the ascii magic number.

    const alphabet = Array.from(Array(26), (e, i) => String.fromCharCode(i + 97));
    

    Again, 97 may be interchanged with 65 for an uppercase alphabet.

    The array may also be initialized with values using the object's keys method rather than utilising the index of the map

    const alphabet = Array.from(Array(26).keys(), i => String.fromCharCode(i + 97));
    

提交回复
热议问题