在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 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
其他回答
这是我的2美分:
function range(start, end) {
return Array.apply(0, Array(end - 1))
.map((element, index) => index + start);
}
没有一个示例进行了测试,每个步骤都有一个生成递减值的选项。
export function range(start = 0, end = 0, step = 1) {
if (start === end || step === 0) {
return [];
}
const diff = Math.abs(end - start);
const length = Math.ceil(diff / step);
return start > end
? Array.from({length}, (value, key) => start - key * step)
: Array.from({length}, (value, key) => start + key * step);
}
测验:
import range from './range'
describe('Range', () => {
it('default', () => {
expect(range()).toMatchObject([]);
})
it('same values', () => {
expect(range(1,1)).toMatchObject([]);
})
it('step=0', () => {
expect(range(0,1,0)).toMatchObject([]);
})
describe('step=1', () => {
it('normal', () => {
expect(range(6,12)).toMatchObject([6, 7, 8, 9, 10, 11]);
})
it('reversed', () => {
expect(range(12,6)).toMatchObject([12, 11, 10, 9, 8, 7]);
})
})
describe('step=5', () => {
it('start 0 end 60', () => {
expect(range(0, 60, 5)).toMatchObject([0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55]);
})
it('reversed start 60 end -1', () => {
expect(range(55, -1, 5)).toMatchObject([55, 50, 45, 40, 35, 30, 25, 20, 15, 10, 5, 0]);
})
})
})
您还可以执行以下操作:
const range = Array.from(Array(size)).map((el, idx) => idx+1).slice(begin, end);
已经给出了非常好的答案,但我没有看到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]
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]