在Ruby中,我可以('a'..'z').to_a
并获得['a', 'b', 'c', 'd', ... 'z']
。
jQuery或Javascript是否提供了类似的构造?
答案 0 :(得分:91)
我个人认为最好的是:
alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('');
简洁,有效,清晰,简单!
修改强> 我已经决定,因为我的答案得到了相当多的关注,所以要添加选择特定字母范围的功能。
function to_a(c1 = 'a', c2 = 'z') {
a = 'abcdefghijklmnopqrstuvwxyz'.split('');
return (a.slice(a.indexOf(c1), a.indexOf(c2) + 1));
}
答案 1 :(得分:28)
如果你需要很多,你可以轻松地为你做功能
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;
}
genCharArray('a', 'z'); // ["a", ..., "z"]
答案 2 :(得分:20)
new Array( 26 ).fill( 1 ).map( ( _, i ) => String.fromCharCode( 65 + i ) );
使用97而不是65来获取小写字母。
答案 3 :(得分:16)
万一有人来这里寻找可以进行硬编码的东西,就去吧:
["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"]
答案 4 :(得分:8)
ES6的简短版本:
const alphabet = [...'abcdefghijklmnopqrstuvwxyz']
答案 5 :(得分:6)
通过使用ES6传播算子,您可以执行以下操作:
let alphabet = [...Array(26).keys()].map(i => String.fromCharCode(i + 97));
答案 6 :(得分:4)
我在上面看到了一个我喜欢的答案,它是英文字母的硬编码列表,但它仅是小写字母,我也需要大写,所以我决定对它进行修改,以防其他人需要它:
const lowerAlph = ["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"];
const upperCaseAlp = ["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"];
答案 7 :(得分:3)
许多这样的答案要么使用字符数组,要么使用String.fromCharCode
,我提出了一种稍微不同的方法,该方法利用了base36中的字母:
[...Array(26)].map((e,i)=>(i+10).toString(36))
此代码的优点是纯粹编码高尔夫,它使用的字符少于其他代码。
答案 8 :(得分:1)
没有Javascript或Jquery不提供类似的东西。你必须创建自己的数组。
您可以尝试这样:
var alpha = ["a","b","c",....];
或者更好的尝试:
var index = 97;
$("#parent .number").each(function(i) {
$(this).html(String.fromCharCode(index++));
});
<强> DEMO 强>
答案 9 :(得分:1)
这是一个快速的单线
无依赖!
Array.from(Array(26)).map((e, i) => i + 65).map((x) => String.fromCharCode(x));
console.log(Array.from(Array(26)).map((e, i) => i + 65).map((x) => String.fromCharCode(x)));
答案 10 :(得分:1)
尝试
[...Array(26)].map((x,i)=>String.fromCharCode(i + 97))
let alphabet = [...Array(26)].map((x,i)=>String.fromCharCode(i + 97));
console.log(alphabet);
答案 11 :(得分:1)
添加到@Sherwin Ablaña Dapito 解决方案(我喜欢在答案中有一些提示)
function (i){ return i+ 65;}
String.fromcharcode
不言自明new Array( 26 ).fill( 1 ).map( ( _, i ) => String.fromCharCode( 65 + i ) );
答案 12 :(得分:0)
试试这个
let name = ''
for(let i=0; i<26; i++){
name+=(i+10).toString(36)
}
console.log(name.split(''))
答案 13 :(得分:0)
[...8337503854730415241050377135811259267835n.toString(36)]
// chrome & firefox
let a = [...8337503854730415241050377135811259267835n.toString(36)];
console.log(a);
答案 14 :(得分:0)
只是为了好玩,然后您可以在Array原型上定义一个吸气剂:
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));
}
});
这使得可以执行以下操作:
['a', 'z'].to_a; // [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", ..., "z" ]
答案 15 :(得分:0)
const charList = (a,z,d=1)=>(a=a.charCodeAt(),z=z.charCodeAt(),[...Array(Math.floor((z-a)/d)+1)].map((_,i)=>String.fromCharCode(a+i*d)));
console.log("from A to G", charList('A', 'G'));
console.log("from A to Z with step/delta of 2", charList('A', 'Z', 2));
console.log("reverse order from Z to P", charList('Z', 'P', -1));
console.log("from 0 to 5", charList('0', '5', 1));
console.log("from 9 to 5", charList('9', '5', -1));
console.log("from 0 to 8 with step 2", charList('0', '8', 2));
console.log("from α to ω", charList('α', 'ω'));
console.log("Hindi characters from क to ह", charList('क', 'ह'));
console.log("Russian characters from А to Я", charList('А', 'Я'));
对于TypeScript
const charList = (p: string, q: string, d = 1) => {
const a = p.charCodeAt(0),
z = q.charCodeAt(0);
return [...Array(Math.floor((z - a) / d) + 1)].map((_, i) =>
String.fromCharCode(a + i * d)
);
};
答案 16 :(得分:0)
以防您需要硬编码的array
字母,但输入较少的情况。上面提到的替代解决方案。
var arr = "abcdefghijklmnopqrstuvwxyz".split("");
将输出这样的数组
/* ["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"] */
答案 17 :(得分:0)
使用JavaScript的Array.from语法,您可以创建一个数组并在每个数组元素上执行映射功能。创建一个新的长度为26的数组,并在每个元素上设置值,该值等于从当前元素的索引的char代码获得的字符串加上ascii幻数。
const alphabet = Array.from(Array(26), (e, i) => String.fromCharCode(i + 97));
同样,对于大写字母,可以将97与65互换。
还可以使用对象的keys方法(而不是利用地图的索引)使用值来初始化数组
const alphabet = Array.from(Array(26).keys(), i => String.fromCharCode(i + 97));