在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本机是否有内置的功能?如果没有,我将如何实施?
当前回答
编码为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]
其他回答
我正在分享我的实现,以防它对某人有所帮助。
function Range(start_or_num, end = null, increment = 1) {
const end_check = end === null
const start = end_check ? 0 : start_or_num
const count = end_check ? start_or_num : Math.round((end - start) / increment) + 1
const filterFunc = end_check ? x => x >= start : x => x < end && x >= start
return [...Array.from(
Array(count).keys(), x => increment * (x - 1) + start
)
].filter(filterFunc)
}
// usage
// console.log(Range(4, 10, 2)) // [4, 6, 8]
// console.log(Range(5, 10 )) //[5, 6, 7, 8, 9]
// console.log(Range(10 ))// [0, 1, 2, 3, 4, 5, 6, 7, 8]
没有一个示例进行了测试,每个步骤都有一个生成递减值的选项。
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]);
})
})
})
使用Harmony生成器,除IE11外,所有浏览器都支持:
var take = function (amount, generator) {
var a = [];
try {
while (amount) {
a.push(generator.next());
amount -= 1;
}
} catch (e) {}
return a;
};
var takeAll = function (gen) {
var a = [],
x;
try {
do {
x = a.push(gen.next());
} while (x);
} catch (e) {}
return a;
};
var range = (function (d) {
var unlimited = (typeof d.to === "undefined");
if (typeof d.from === "undefined") {
d.from = 0;
}
if (typeof d.step === "undefined") {
if (unlimited) {
d.step = 1;
}
} else {
if (typeof d.from !== "string") {
if (d.from < d.to) {
d.step = 1;
} else {
d.step = -1;
}
} else {
if (d.from.charCodeAt(0) < d.to.charCodeAt(0)) {
d.step = 1;
} else {
d.step = -1;
}
}
}
if (typeof d.from === "string") {
for (let i = d.from.charCodeAt(0); (d.step > 0) ? (unlimited ? true : i <= d.to.charCodeAt(0)) : (i >= d.to.charCodeAt(0)); i += d.step) {
yield String.fromCharCode(i);
}
} else {
for (let i = d.from; (d.step > 0) ? (unlimited ? true : i <= d.to) : (i >= d.to); i += d.step) {
yield i;
}
}
});
示例
take
示例1。
尽可能多地索取
take(10,范围({从:100,步骤:5,到:120}))
回报
[100, 105, 110, 115, 120]
示例2。
不需要
take(10,范围({从:100,步骤:5}))
回报
[100, 105, 110, 115, 120, 125, 130, 135, 140, 145]
全部接受
示例3。
来自不必要的
takeAll(范围({到:5}))
回报
[0, 1, 2, 3, 4, 5]
示例4。
takeAll(范围({到:500,步骤:100}))
回报
[0, 100, 200, 300, 400, 500]
示例5。
takeAll(范围({从:“z”到:“a”}))
回报
[“z”、“y”、“x”、“w”、“v”、“u”、“t”、“s”、“r”、“q”、“p”、“o”、“n”、“m”、“l”、“k”、“j”、“i”、“h”、“g”、“f”、“e”、“d”、“c”、“b”、“a”]
我刚刚通过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
我的代码高尔夫同事想出了这个(ES6),包容的:
(s,f)=>[...Array(f-s+1)].map((e,i)=>i+s)
非包容性:
(s,f)=>[...Array(f-s)].map((e,i)=>i+s)