How to generate an array of alphabet in jQuery?
A short ES6 version:
const alphabet = [...'abcdefghijklmnopqrstuvwxyz'];
console.log(alphabet);
Personally I think the best is:
alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('');
Concise, effective, legible, and simple!
EDIT: I have decided, that since my answer is receiving a fair amount of attention to add the functionality to choose specific ranges of letters.
function to_a(c1 = 'a', c2 = 'z') {
a = 'abcdefghijklmnopqrstuvwxyz'.split('');
return (a.slice(a.indexOf(c1), a.indexOf(c2) + 1));
}
console.log(to_a('b', 'h'));
In case anyone came here looking for something they can hard-code, here you go:
["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
You can easily make a function to do this for you if you'll need it a lot
function genCharArray(charA, charZ) {
var a = [], i = charA.charCodeAt(0), j = charZ.charCodeAt(0);
for (; i <= j; ++i) {
a.push(String.fromCharCode(i));
}
return a;
}
console.log(genCharArray('a', 'z')); // ["a", ..., "z"]