在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本机是否有内置的功能?如果没有,我将如何实施?
当前回答
已经给出了非常好的答案,但我没有看到ES6迭代器的完整使用来实现范围的完整实现,所以这里是:
/**
* inspired by Python's built-in range utility function
* implemented using ES6 Iterable, Iterator protolols (interfaces)
*/
class Range {
constructor(...args) {
this.start = args.length <= 1 ? 0 : args[0];
this.end = args.length <= 1 ? args[0] || 0 : args[1];
this.step = args.length <= 2 ? 1 : args[2];
}
[Symbol.iterator]() {
return this;
}
next() {
if (this.end > this.start) {
const result = { done: false, value: this.start };
this.start = this.start + this.step;
return result;
} else return { done: true, value: undefined };
}
}
/**
* Symbol.iterator is part of the couple of inbuilt symbols
* available in JavaScript. It allows for hooking into the
* inbuilt for of iteration mechanism. This is why the
* Symbol.iterator comes into play when talking about
* iterables and iterators in JavaScript.
*/
function range(...args) {
return new Range(...args);
}
console.log([...range(4)]); // [0, 1, 2, 3]
console.log([...range(2, 5)]); // [2, 3, 4]
console.log([...range(1, 10, 3)]); // [1, 4, 7]
其他回答
(from, to) => [...Array(to - from)].map((_,i)=> i + from)
我发现了一个与PHP中的函数相当的JS范围函数,在这里工作得非常棒。向前和向后工作,可以处理整数、浮点数和字母!
function range(low, high, step) {
// discuss at: http://phpjs.org/functions/range/
// original by: Waldo Malqui Silva
// example 1: range ( 0, 12 );
// returns 1: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
// example 2: range( 0, 100, 10 );
// returns 2: [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
// example 3: range( 'a', 'i' );
// returns 3: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
// example 4: range( 'c', 'a' );
// returns 4: ['c', 'b', 'a']
var matrix = [];
var inival, endval, plus;
var walker = step || 1;
var chars = false;
if (!isNaN(low) && !isNaN(high)) {
inival = low;
endval = high;
} else if (isNaN(low) && isNaN(high)) {
chars = true;
inival = low.charCodeAt(0);
endval = high.charCodeAt(0);
} else {
inival = (isNaN(low) ? 0 : low);
endval = (isNaN(high) ? 0 : high);
}
plus = ((inival > endval) ? false : true);
if (plus) {
while (inival <= endval) {
matrix.push(((chars) ? String.fromCharCode(inival) : inival));
inival += walker;
}
} else {
while (inival >= endval) {
matrix.push(((chars) ? String.fromCharCode(inival) : inival));
inival -= walker;
}
}
return matrix;
}
这是缩小版:
function range(h,c,b){var i=[];var d,f,e;var a=b||1;var g=false;if(!isNaN(h)&&!isNaN(c)){d=h;f=c}else{if(isNaN(h)&&isNaN(c)){g=true;d=h.charCodeAt(0);f=c.charCodeAt(0)}else{d=(isNaN(h)?0:h);f=(isNaN(c)?0:c)}}e=((d>f)?false:true);if(e){while(d<=f){i.push(((g)?String.fromCharCode(d):d));d+=a}}else{while(d>=f){i.push(((g)?String.fromCharCode(d):d));d-=a}}return i};
d3还具有内置范围功能。
看见https://github.com/d3/d3-array#range:
d3.范围([开始,]停止[,步骤])
生成包含算术级数的数组,类似于Python内置范围。此方法通常用于迭代一系列数值或整数值,例如数组中的索引。与Python版本不同,参数不需要是整数,但如果是由于浮点精度,则结果更可预测。如果省略步骤,则默认为1。
例子:
d3.range(10)
// returns [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
我喜欢下面的路
var范围=函数(x,y){return数组(y-x+1).fill(x).map((a,b)=>{return a+b}).filter(i=>i>=x);};console.log(范围(3,10));
标准的Javascript没有生成范围的内置函数。有几个javascript框架添加了对这些功能的支持,或者正如其他人所指出的那样,您可以一直使用自己的功能。
如果您想再次检查,确定的资源是ECMA-262标准。