在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本机是否有内置的功能?如果没有,我将如何实施?
当前回答
数字
[...Array(5).keys()];
=> [0, 1, 2, 3, 4]
字符迭代
String.fromCharCode(...[...Array('D'.charCodeAt(0) - 'A'.charCodeAt(0) + 1).keys()].map(i => i + 'A'.charCodeAt(0)));
=> "ABCD"
迭代
for (const x of Array(5).keys()) {
console.log(x, String.fromCharCode('A'.charCodeAt(0) + x));
}
=> 0,"A" 1,"B" 2,"C" 3,"D" 4,"E"
作为函数
function range(size, startAt = 0) {
return [...Array(size).keys()].map(i => i + startAt);
}
function characterRange(startChar, endChar) {
return String.fromCharCode(...range(endChar.charCodeAt(0) -
startChar.charCodeAt(0), startChar.charCodeAt(0)))
}
类型化函数
function range(size:number, startAt:number = 0):ReadonlyArray<number> {
return [...Array(size).keys()].map(i => i + startAt);
}
function characterRange(startChar:string, endChar:string):ReadonlyArray<string> {
return String.fromCharCode(...range(endChar.charCodeAt(0) -
startChar.charCodeAt(0), startChar.charCodeAt(0)))
}
lodash.js_.range()函数
_.range(10);
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
_.range(1, 11);
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
_.range(0, 30, 5);
=> [0, 5, 10, 15, 20, 25]
_.range(0, -10, -1);
=> [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
String.fromCharCode(..._.range('A'.charCodeAt(0), 'D'.charCodeAt(0) + 1));
=> "ABCD"
没有库的旧非es6浏览器:
Array.apply(null, Array(5)).map(function (_, i) {return i;});
=> [0, 1, 2, 3, 4]
console.log([…Array(5).keys()]);
(ES6归功于尼尔斯·彼得索恩和其他评论者)
其他回答
也可以使用生成器生成序列。不同之处在于序列中的每个值都是延迟加载的。spread运算符和for为结果工作。星号符号使函数成为生成器。
const range = function*(from,to) {
for(let i = from; i <= to; i++) yield I;
};
[...range(3,5)]// => [3, 4, 5]
我刚刚通过Object.defineProperty在Array上创建了这个polyfill,以创建整数或字符串的范围。Object.defineProperty是创建polyfills的一种更安全的方法。
更安全的polyfill
if (!Array.range) {
Object.defineProperty(Array, 'range', {
value: function (from, to, step) {
if (typeof from !== 'number' && typeof from !== 'string') {
throw new TypeError('The first parameter should be a number or a character')
}
if (typeof to !== 'number' && typeof to !== 'string') {
throw new TypeError('The second parameter should be a number or a character')
}
var A = []
if (typeof from === 'number') {
A[0] = from
step = step || 1
while (from + step <= to) {
A[A.length] = from += step
}
} else {
var s = 'abcdefghijklmnopqrstuvwxyz'
if (from === from.toUpperCase()) {
to = to.toUpperCase()
s = s.toUpperCase()
}
s = s.substring(s.indexOf(from), s.indexOf(to) + 1)
A = s.split('')
}
return A
}
})
} else {
var errorMessage = 'DANGER ALERT! Array.range has already been defined on this browser. '
errorMessage += 'This may lead to unwanted results when Array.range() is executed.'
console.log(errorMessage)
}
示例
Array.range(1, 3)
// Return: [1, 2, 3]
Array.range(1, 3, 0.5)
// Return: [1, 1.5, 2, 2.5, 3]
Array.range('a', 'c')
// Return: ['a', 'b', 'c']
Array.range('A', 'C')
// Return: ['A', 'B', 'C']
Array.range(null)
Array.range(undefined)
Array.range(NaN)
Array.range(true)
Array.range([])
Array.range({})
Array.range(1, null)
// Return: Uncaught TypeError: The X parameter should be a number or a character
// range() 0..10, step=1
// range(max) 0..max, step=1
// range(min,max) min..max, step=1
// range(min,step,max) min..max, step=step
// Use:
// console.log(...range(3));
// Array.from(range(5))
// [...range(100)]
// for (const v of range(1,10)) { ...
function* range(...args) {
let [min, step, max] = {
0: [0, 1, 10],
1: [0, args[0] >= 0 ? 1 : -1, args[0]],
2: [args[0], args[1] >= args[0] ? 1 : -1, args[1]],
3: args,
}[args.length] || [];
if (min === undefined) throw new SyntaxError("Too many arguments");
let x = min;
while (step >= 0 ? x < max : x > max) {
yield x;
x += step
}
}
console.log(...range()); // 0 1 2 3 4 5 6 7 8 9
console.log(...range(3)); // 0 1 2
console.log(...range(2, 5)); // 2 3 4
console.log(...range(5, 2)); // 5 4 3
console.log(...range(3, -3)); // 3 2 1 0 -1 -2
console.log(...range(-3, 3)); // -3 -2 -1 0 1 2
console.log(...range(-5, -2));// -5 -4 -3
console.log(...range(-2, -5));// -2 -3 -4
这是我用于数字范围的方法:
const rangeFrom0 = end => [...Array(end)].map((_, index) => index);
or
const rangeExcEnd = (start, step, end) => [...Array(end - start + 1)]
.map((_, index) => index + start)
.filter(x => x % step === start % step);
我的代码高尔夫同事想出了这个(ES6),包容的:
(s,f)=>[...Array(f-s+1)].map((e,i)=>i+s)
非包容性:
(s,f)=>[...Array(f-s)].map((e,i)=>i+s)