【发布时间】:2014-08-27 04:05:41
【问题描述】:
在 Ruby 中,我可以使用 ('a'..'z').to_a 并获得 ['a', 'b', 'c', 'd', ... 'z']。
jQuery 或 Javascript 是否提供类似的构造?
【问题讨论】:
-
不,您需要自己创建。
标签: javascript jquery
在 Ruby 中,我可以使用 ('a'..'z').to_a 并获得 ['a', 'b', 'c', 'd', ... 'z']。
jQuery 或 Javascript 是否提供类似的构造?
【问题讨论】:
标签: javascript jquery
没有 Javascript 或 Jquery 不提供类似的东西。您必须创建自己的数组。
你可以这样尝试:
var alpha = ["a","b","c",....];
或者最好这样尝试:
var index = 97;
$("#parent .number").each(function(i) {
$(this).html(String.fromCharCode(index++));
});
【讨论】:
$.map() 创建一个数组。 $.map(Array(26), function(_, i) { return String.fromCharCode(i + 97); })jsfiddle.net/agKrz/18
如果您非常需要它,您可以轻松地创建一个函数来为您执行此操作
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"]
【讨论】:
我个人认为最好的是:
alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('');
简洁、有效、易读、简单!
编辑: 我已经决定,因为我的回答受到了相当多的关注,因此添加了选择特定字母范围的功能。
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'));
【讨论】:
('m'..'p').to_a、('A'..'F').to_a 或('1'..'5').to_a?
const alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('').map((c) => c.toUpperCase());
to_a('A', 'z'); 的预期输出是什么?还是to_a('α', 'ω')@MichaelLonghurst?另外@sonlexqt,如果您以其他方式订购操作,您可以节省大量 CPU; 'abcdefghijklmnopqrstuvwxyz'.toUpperCase().split('')
new Array( 26 ).fill( 1 ).map( ( _, i ) => String.fromCharCode( 65 + i ) );
使用 97 而不是 65 来获取小写字母。
【讨论】:
'A'.charCodeAt(0) 删除一个幻数(以很小的性能成本)
如果有人来这里寻找他们可以硬编码的东西,你去吧:
["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"]
【讨论】:
通过使用 ES6 扩展运算符,您可以执行以下操作:
let alphabet = [...Array(26).keys()].map(i => String.fromCharCode(i + 97));
【讨论】:
使用 JavaScript 的 Array.from 语法,您可以创建一个数组并对每个数组元素执行映射函数。创建一个长度为 26 的新数组,并在每个元素上设置等于从当前元素索引的 char 码加上 ascii 幻数获得的字符串。
const alphabet = Array.from(Array(26), (e, i) => String.fromCharCode(i + 97));
同样,对于大写字母,97 可以与 65 互换。
数组也可以使用对象的键方法而不是使用映射的索引来初始化值
const alphabet = Array.from(Array(26).keys(), i => String.fromCharCode(i + 97));
【讨论】:
我在上面看到了一个我喜欢的答案,它是英文字母的硬编码列表,但它只是小写的,我也需要大写,所以我决定修改它以防其他人需要它:
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"];
【讨论】:
['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'](从你的复制而来,不知何故你有不同的代码风格)
很多这些答案要么使用字符数组,要么使用String.fromCharCode,我提出了一种稍微不同的方法,利用base36中的字母:
[...Array(26)].map((e,i)=>(i+10).toString(36))
这个的优点是纯代码高尔夫,它使用的字符比其他的少。
【讨论】:
.toString(36) 是字母表吗?
.toString(36) 将基数为 10 的数字转换为基数 36。此代码创建一个由 10-35 组成的数字数组,并将它们每个转换为基数 36。10->A、11->B ... 35->Z。
toString(36) 之后追加.toUpperCase()...
如果您需要一个硬编码的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"] */
【讨论】:
一个简短的 ES6 版本:
const alphabet = [...'abcdefghijklmnopqrstuvwxyz'];
console.log(alphabet);
【讨论】:
const ALPHA = Array.from({ length: 26 }, (_, i) => String.fromCharCode('a'.charCodeAt(0) + i)); // ['a', 'b', ...'z']
我相信上面的代码更惯用。足够短,可以作为内联代码。 您不必记住起始字母的 charCode,并且可以通过简单地控制长度和起始字母来配置来检索字母表的子集,例如
Array.from({ length: 3 }, (_, i) => String.fromCharCode('x'.charCodeAt(0) + i)) // ['x', 'y', 'z]
【讨论】:
试试
[...Array(26)].map((x,i)=>String.fromCharCode(i + 97))
let alphabet = [...Array(26)].map((x,i)=>String.fromCharCode(i + 97));
console.log(alphabet);
更新
正如您在 cmets 中注意到的那样,这个想法已经在 this answer 中使用(我错过了) - 但这个答案更短,所以将其视为旧答案的大小改进
【讨论】:
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('А', 'Я'));
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)
);
};
【讨论】:
只是为了好玩,那么你可以在 Array 原型上定义一个 getter:
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" ]
【讨论】:
[...8337503854730415241050377135811259267835n.toString(36)]
// chrome & firefox
let a1 = [...8337503854730415241050377135811259267835n.toString(36)];
console.log(a1);
// version working on all browsers (without using BigInt)
let a2 = [...[37713647386641440,2196679683172530,53605115].map(x=>x.toString(36)).join``];
console.log(a2);
【讨论】:
添加到@Sherwin Ablaña Dapito 解决方案(我喜欢在答案中提供一些提示)
function (i){ return i+ 65;}
String.fromcharcode 不言自明new Array( 26 ).fill( 1 ).map( ( _, i ) => String.fromCharCode( 65 + i ) );
【讨论】:
这是一个快速的单线
没有依赖关系!
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)));
【讨论】:
试试这个
let name = ''
for(let i=0; i<26; i++){
name+=(i+10).toString(36)
}
console.log(name.split(''))
【讨论】:
//使用地图1行代码
let alphabet =[...Array(26)].map( (_,i) => String.fromCharCode(65+i) )
// 使用数组插入 A-Z
let newArray = []
for(var i = 0; i<26 ; i++){
newArray.push(String.fromCharCode(65+i))
}
// 使用数组插入 a-z
let newArray = []
for(var i = 0`enter code here`; i<26 ; i++){
newArray.push(String.fromCharCode(65+i).toLowerCase())
}
//使用拆分
let a = 'abcdefghijklmnopqrstuvwxyz'.split('');
【讨论】: