假设我有以下复选框:
<input type="checkbox" value="1-25" />
为了得到定义我正在寻找的范围边界的两个数字,我使用下面的jQuery:
var value = $(this).val();
var lowEnd = Number(value.split('-')[0]);
var highEnd = Number(value.split('-')[1]);
然后,我如何创建一个包含lowEnd和highEnd之间的所有整数的数组,包括lowEnd和highEnd本身?对于这个特定的例子,显然,结果数组将是:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
您可以设计一个range方法,使“从”数增加所需的数量,直到达到“到”数。
这个例子将“计数”向上或向下,这取决于from比to大还是比to小。
Array.range= function(from, to, step){
if(typeof from== 'number'){
var A= [from];
step= typeof step== 'number'? Math.abs(step):1;
if(from> to){
while((from -= step)>= to) A.push(from);
}
else{
while((from += step)<= to) A.push(from);
}
return A;
}
}
如果你想步进一个小数:Array.range(0,1,01)
您将需要截断任何浮点不精确的值。
否则你会返回类似的数字
0.060000000000000005而不是。06。
这给另一个版本增加了一点开销,但对于整数或十进制步骤是正确的。
Array.range= function(from, to, step, prec){
if(typeof from== 'number'){
var A= [from];
step= typeof step== 'number'? Math.abs(step):1;
if(!prec){
prec= (from+step)%1? String((from+step)%1).length+1:0;
}
if(from> to){
while(+(from -= step).toFixed(prec)>= to) A.push(+from.toFixed(prec));
}
else{
while(+(from += step).toFixed(prec)<= to) A.push(+from.toFixed(prec));
}
return A;
}
}
您可以设计一个range方法,使“从”数增加所需的数量,直到达到“到”数。
这个例子将“计数”向上或向下,这取决于from比to大还是比to小。
Array.range= function(from, to, step){
if(typeof from== 'number'){
var A= [from];
step= typeof step== 'number'? Math.abs(step):1;
if(from> to){
while((from -= step)>= to) A.push(from);
}
else{
while((from += step)<= to) A.push(from);
}
return A;
}
}
如果你想步进一个小数:Array.range(0,1,01)
您将需要截断任何浮点不精确的值。
否则你会返回类似的数字
0.060000000000000005而不是。06。
这给另一个版本增加了一点开销,但对于整数或十进制步骤是正确的。
Array.range= function(from, to, step, prec){
if(typeof from== 'number'){
var A= [from];
step= typeof step== 'number'? Math.abs(step):1;
if(!prec){
prec= (from+step)%1? String((from+step)%1).length+1:0;
}
if(from> to){
while(+(from -= step).toFixed(prec)>= to) A.push(+from.toFixed(prec));
}
else{
while(+(from += step).toFixed(prec)<= to) A.push(+from.toFixed(prec));
}
return A;
}
}
function range(j, k) {
return Array
.apply(null, Array((k - j) + 1))
.map(function(_, n){ return n + j; });
}
这大致相当于
function range(j, k) {
var targetLength = (k - j) + 1;
var a = Array(targetLength);
var b = Array.apply(null, a);
var c = b.map(function(_, n){ return n + j; });
return c;
}
分解一下:
var targetLength = (k - j) + 1;
var a = Array(targetLength);
这将创建一个具有正确标称长度的稀疏矩阵。现在,稀疏矩阵的问题是尽管它有正确的标称长度,但它没有实际的元素,所以,对于
j = 7, k = 13
console.log(a);
给了我们
Array [ <7 empty slots> ]
Then
var b = Array.apply(null, a);
将稀疏矩阵作为参数列表传递给Array构造函数,该构造函数生成一个(实际)长度为targetLength的密集矩阵,其中所有元素都具有未定义的值。第一个参数是数组构造函数执行上下文的“this”值,在这里没有作用,因此为null。
现在,
console.log(b);
收益率
Array [ undefined, undefined, undefined, undefined, undefined, undefined, undefined ]
最后
var c = b.map(function(_, n){ return n + j; });
利用了数组。映射函数通过:当前元素的值和2。当前元素的索引,映射委托/回调。第一个参数将被丢弃,而第二个参数在调整开始偏移量后可用于设置正确的序列值。
然后
console.log(c);
收益率
Array [ 7, 8, 9, 10, 11, 12, 13 ]
我的五分钱:
两个方向的整数数组函数。
当range(0,5)变为[0,1,2,3,4,5]时。
范围(5,0)变为[5,4,3,2,1,0]。
基于这个答案。
function range(start, end) {
const isReverse = (start > end);
const targetLength = isReverse ? (start - end) + 1 : (end - start ) + 1;
const arr = new Array(targetLength);
const b = Array.apply(null, arr);
const result = b.map((discard, n) => {
return (isReverse) ? n + end : n + start;
});
return (isReverse) ? result.reverse() : result;
}
附注:为了在现实生活中使用,你还应该检查isFinite()和isNaN()的参数。