在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本机是否有内置的功能?如果没有,我将如何实施?
当前回答
没有一个示例进行了测试,每个步骤都有一个生成递减值的选项。
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 stepScale = (min, max, numberOfSteps) => {
const _numberOfSteps = numberOfSteps - 1
const scaleBy = (max - min) / _numberOfSteps
const arr = []
for (let i = 0; i <= _numberOfSteps; i += 1) {
arr.push(min + scaleBy * i)
}
return arr
}
export default stepScale
stepScale(5, 10, 4)
// [5, 6.666666666666667, 8.333333333333334, 10]
对于npmhttps://npm.im/@通用标准/步长
我的代码高尔夫同事想出了这个(ES6),包容的:
(s,f)=>[...Array(f-s+1)].map((e,i)=>i+s)
非包容性:
(s,f)=>[...Array(f-s)].map((e,i)=>i+s)
编码为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]
已经给出了非常好的答案,但我没有看到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]
我的实施
export function stringRange(a: string, b: string) {
let arr = [a + ''];
const startPrefix = a.match(/([\D])+/g);
const endPrefix = b.match(/([\D])+/g);
if ((startPrefix || endPrefix) && (Array.isArray(startPrefix) && startPrefix[0]) !== (Array.isArray(endPrefix) && endPrefix[0])) {
throw new Error('Series number does not match');
}
const startNum = a.match(/([\d])+/g);
const endNum = b.match(/([\d])+/g);
if (!startNum || !endNum) {
throw new Error('Range is not valid');
}
let start = parseInt(startNum[0], 10);
let end = parseInt(endNum[0], 10);
if (start > end) {
throw new Error('Ending value should be lessesr that starting value');
}
while (start !== end) {
start++;
arr.push(startPrefix ? startPrefix[0] + (start + '').padStart(startNum[0].length, '0') : start + '');
}
return arr;
}
样本结果
// console.log(range('0', '10'));
// console.log(range('10', '10'));
// console.log(range('10', '20'));
// console.log(range('10', '20000'));
// console.log(range('ABC10', 'ABC23'));
// console.log(range('ABC10', 'ABC2300'));
// console.log(range('ABC10', 'ABC09')); --> Failure case
// console.log(range('10', 'ABC23')); --> Failure case
// console.log(range('ABC10', '23')); --> Failure case