我正在寻找以下方法的任何替代方法,以创建包含1到N的JavaScript数组,其中N仅在运行时已知。
var foo = [];
for (var i = 1; i <= N; i++) {
foo.push(i);
}
对我来说,我觉得应该有一种不用循环的方法。
我正在寻找以下方法的任何替代方法,以创建包含1到N的JavaScript数组,其中N仅在运行时已知。
var foo = [];
for (var i = 1; i <= N; i++) {
foo.push(i);
}
对我来说,我觉得应该有一种不用循环的方法。
当前回答
https://stackoverflow.com/a/49577331/8784402
使用Delta
对于javascript
smallest and one-liner[...Array(N)].map((v, i) => from + i * step);
示例和其他备选方案
Array.from(Array(10).keys()).map(i => 4 + i * 2);
//=> [4, 6, 8, 10, 12, 14, 16, 18, 20, 22]
[...Array(10).keys()].map(i => 4 + i * -2);
//=> [4, 2, 0, -2, -4, -6, -8, -10, -12, -14]
Array(10).fill(0).map((v, i) => 4 + i * 2);
//=> [4, 6, 8, 10, 12, 14, 16, 18, 20, 22]
Array(10).fill().map((v, i) => 4 + i * -2);
//=> [4, 2, 0, -2, -4, -6, -8, -10, -12, -14]
[...Array(10)].map((v, i) => 4 + i * 2);
//=> [4, 6, 8, 10, 12, 14, 16, 18, 20, 22]
Range Function
const range = (from, to, step) =>
[...Array(Math.floor((to - from) / step) + 1)].map((_, i) => from + i * step);
range(0, 9, 2);
//=> [0, 2, 4, 6, 8]
// can also assign range function as static method in Array class (but not recommended )
Array.range = (from, to, step) =>
[...Array(Math.floor((to - from) / step) + 1)].map((_, i) => from + i * step);
Array.range(2, 10, 2);
//=> [2, 4, 6, 8, 10]
Array.range(0, 10, 1);
//=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Array.range(2, 10, -1);
//=> []
Array.range(3, 0, -1);
//=> [3, 2, 1, 0]
As Iterators
class Range {
constructor(total = 0, step = 1, from = 0) {
this[Symbol.iterator] = function* () {
for (let i = 0; i < total; yield from + i++ * step) {}
};
}
}
[...new Range(5)]; // Five Elements
//=> [0, 1, 2, 3, 4]
[...new Range(5, 2)]; // Five Elements With Step 2
//=> [0, 2, 4, 6, 8]
[...new Range(5, -2, 10)]; // Five Elements With Step -2 From 10
//=>[10, 8, 6, 4, 2]
[...new Range(5, -2, -10)]; // Five Elements With Step -2 From -10
//=> [-10, -12, -14, -16, -18]
// Also works with for..of loop
for (i of new Range(5, -2, 10)) console.log(i);
// 10 8 6 4 2
As Generators Only
const Range = function* (total = 0, step = 1, from = 0) {
for (let i = 0; i < total; yield from + i++ * step) {}
};
Array.from(Range(5, -2, -10));
//=> [-10, -12, -14, -16, -18]
[...Range(5, -2, -10)]; // Five Elements With Step -2 From -10
//=> [-10, -12, -14, -16, -18]
// Also works with for..of loop
for (i of Range(5, -2, 10)) console.log(i);
// 10 8 6 4 2
// Lazy loaded way
const number0toInf = Range(Infinity);
number0toInf.next().value;
//=> 0
number0toInf.next().value;
//=> 1
// ...
带步长/增量的从到
using iteratorsclass Range2 {
constructor(to = 0, step = 1, from = 0) {
this[Symbol.iterator] = function* () {
let i = 0,
length = Math.floor((to - from) / step) + 1;
while (i < length) yield from + i++ * step;
};
}
}
[...new Range2(5)]; // First 5 Whole Numbers
//=> [0, 1, 2, 3, 4, 5]
[...new Range2(5, 2)]; // From 0 to 5 with step 2
//=> [0, 2, 4]
[...new Range2(5, -2, 10)]; // From 10 to 5 with step -2
//=> [10, 8, 6]
using Generators
const Range2 = function* (to = 0, step = 1, from = 0) {
let i = 0,
length = Math.floor((to - from) / step) + 1;
while (i < length) yield from + i++ * step;
};
[...Range2(5, -2, 10)]; // From 10 to 5 with step -2
//=> [10, 8, 6]
let even4to10 = Range2(10, 2, 4);
even4to10.next().value;
//=> 4
even4to10.next().value;
//=> 6
even4to10.next().value;
//=> 8
even4to10.next().value;
//=> 10
even4to10.next().value;
//=> undefined
对于字体
class _Array<T> extends Array<T> {
static range(from: number, to: number, step: number): number[] {
return Array.from(Array(Math.floor((to - from) / step) + 1)).map(
(v, k) => from + k * step
);
}
}
_Array.range(0, 9, 1);
其他回答
最终总结报告。。Drrruummm Rolll公司-
这是在不使用ES6的情况下生成大小为N(此处为10)的数组的最短代码。Cocco上面的版本很接近,但不是最短的。
(function(n){for(a=[];n--;a[n]=n+1);return a})(10)
但这场“代码高尔夫”(用最少的源代码字节来解决特定问题的比赛)无可争议的赢家是Niko Ruotsalainen。使用数组构造函数和ES6扩展运算符。(大多数ES6语法都是有效的typeScript,但下面不是。所以在使用它时要谨慎)
[...Array(10).keys()]
你可以这样做:
var arr=数组.from(数组(10).keys())arr.shift()控制台日志(arr)
对以上内容进行改进:
var range = function (n) {
return Array(n).join().split(',').map(function(e, i) { return i; });
}
可以获得以下选项:
1) Array.init设置为值v
var arrayInitTo = function (n,v) {
return Array(n).join().split(',').map(function() { return v; });
};
2) 获得反向范围:
var rangeRev = function (n) {
return Array(n).join().split(',').map(function() { return n--; });
};
如果您碰巧像我一样在应用程序中使用d3.js,d3会提供一个助手函数来为您执行此操作。
因此,要获得从0到4的数组,非常简单:
d3.range(5)
[0, 1, 2, 3, 4]
并获得从1到5的数组,如您所请求的:
d3.range(1, 5+1)
[1, 2, 3, 4, 5]
查看本教程了解更多信息。
尝试在Number的原型中添加迭代器。
Number.prototype[Symbol.iterator] = function *(){
let i = 0;
while(i < this) yield i++;
return;
}
现在数字是可迭代的,只需将一个数字传递给Array.from
Array.from(10);//[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
或者任何其他需要迭代的地方,比如。。。循环。
for(const number of 10) console.log(number);//logs 0 through 9 sequentially
这有点复杂,但也很酷。