在PHP中,您可以。。。
range(1, 3); // Array(1, 2, 3)
range("A", "C"); // Array("A", "B", "C")
也就是说,有一个函数可以通过传递上下限来获得一系列数字或字符。
JavaScript本机是否有内置的功能?如果没有,我将如何实施?
在PHP中,您可以。。。
range(1, 3); // Array(1, 2, 3)
range("A", "C"); // Array("A", "B", "C")
也就是说,有一个函数可以通过传递上下限来获得一系列数字或字符。
JavaScript本机是否有内置的功能?如果没有,我将如何实施?
当前回答
一个有趣的挑战是编写最短的函数来实现这一点。救援递归!
function r(a,b){return a>b?[]:[a].concat(r(++a,b))}
在大范围内往往速度较慢,但幸运的是量子计算机即将问世。
另一个额外的好处是,它令人困惑。因为我们都知道隐藏代码以防被窥探是多么重要。
要真正彻底混淆功能,请执行以下操作:
function r(a,b){return (a<b?[a,b].concat(r(++a,--b)):a>b?[]:[a]).sort(function(a,b){return a-b})}
其他回答
可以如下创建大量使用ES6的相当简约的实现,特别注意Array.from()静态方法:
const getRange = (start, stop) => Array.from(
new Array((stop - start) + 1),
(_, i) => i + start
);
编码为2010年规格(是的,2016年是ES6发电机)。这是我的想法,其中包含模拟Python的range()函数的选项。
Array.range = function(start, end, step){
if (start == undefined) { return [] } // "undefined" check
if ( (step === 0) ) { return []; // vs. throw TypeError("Invalid 'step' input")
} // "step" == 0 check
if (typeof start == 'number') { // number check
if (typeof end == 'undefined') { // single argument input
end = start;
start = 0;
step = 1;
}
if ((!step) || (typeof step != 'number')) {
step = end < start ? -1 : 1;
}
var length = Math.max(Math.ceil((end - start) / step), 0);
var out = Array(length);
for (var idx = 0; idx < length; idx++, start += step) {
out[idx] = start;
}
// Uncomment to check "end" in range() output, non pythonic
if ( (out[out.length-1] + step) == end ) { // "end" check
out.push(end)
}
} else {
// Historical: '&' is the 27th letter: http://nowiknow.com/and-the-27th-letter-of-the-alphabet/
// Axiom: 'a' < 'z' and 'z' < 'A'
// note: 'a' > 'A' == true ("small a > big A", try explaining it to a kid! )
var st = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ&'; // axiom ordering
if (typeof end == 'undefined') { // single argument input
end = start;
start = 'a';
}
var first = st.indexOf(start);
var last = st.indexOf(end);
if ((!step) || (typeof step != 'number')) {
step = last < first ? -1 : 1;
}
if ((first == -1) || (last == -1 )) { // check 'first' & 'last'
return []
}
var length = Math.max(Math.ceil((last - first) / step), 0);
var out = Array(length);
for (var idx = 0; idx < length; idx++, first += step) {
out[idx] = st[first];
}
// Uncomment to check "end" in range() output, non pythonic
if ( (st.indexOf(out[out.length-1]) + step ) == last ) { // "end" check
out.push(end)
}
}
return out;
}
例子:
Array.range(5); // [0,1,2,3,4,5]
Array.range(4,-4,-2); // [4, 2, 0, -2, -4]
Array.range('a','d'); // ["a", "b", "c", "d"]
Array.range('B','y'); // ["B", "A", "z", "y"], different from chr() ordering
Array.range('f'); // ["a", "b", "c", "d", "e", "f"]
Array.range(-5); // [], similar to python
Array.range(-5,0) // [-5,-4-,-3-,-2,-1,0]
标准的Javascript没有生成范围的内置函数。有几个javascript框架添加了对这些功能的支持,或者正如其他人所指出的那样,您可以一直使用自己的功能。
如果您想再次检查,确定的资源是ECMA-262标准。
使用范围([start,]stop[,step])签名完成ES6实现:
function range(start, stop, step=1){
if(!stop){stop=start;start=0;}
return Array.from(new Array(int((stop-start)/step)), (x,i) => start+ i*step)
}
如果要自动负步进,请添加
if(stop<start)step=-Math.abs(step)
或者更简单地说:
range=(b, e, step=1)=>{
if(!e){e=b;b=0}
return Array.from(new Array(int((e-b)/step)), (_,i) => b<e? b+i*step : b-i*step)
}
如果你有巨大的射程,看看保罗·莫雷蒂的发电机方法
对于数字,您可以使用ES6 Array.from(),它现在可以在除IE以外的任何情况下工作:
较短版本:
Array.from({length: 20}, (x, i) => i);
更长版本:
Array.from(new Array(20), (x, i) => i);
这创建了从0到19(包括0到19)的数组。这可以进一步简化为以下形式之一:
Array.from(Array(20).keys());
// or
[...Array(20).keys()];
也可以指定下限和上限,例如:
Array.from(new Array(20), (x, i) => i + *lowerBound*);
一篇文章对此进行了更详细的描述:http://www.2ality.com/2014/05/es6-array-methods.html