我正在寻找以下方法的任何替代方法,以创建包含1到N的JavaScript数组,其中N仅在运行时已知。

var foo = [];

for (var i = 1; i <= N; i++) {
   foo.push(i);
}

对我来说,我觉得应该有一种不用循环的方法。


当前回答

以下是摘要(在控制台中运行):

// setup:
var n = 10000000;
function* rangeIter(a, b) {
    for (let i = a; i <= b; ++i) yield i;
}
function range(n) { 
    let a = []
    for (; n--; a[n] = n);
    return a;
}
function sequence(max, step = 1) {
    return {
        [Symbol.iterator]: function* () {
            for (let i = 1; i <= max; i += step) yield i
        }
    }
}

var t0, t1, arr;
// tests
t0 = performance.now();
arr = Array.from({ length: n }, (a, i) => 1)
t1 = performance.now();
console.log("Array.from({ length: n }, (a, i) => 1) Took " + (t1 - t0) + " milliseconds.");

t0 = performance.now();
arr = range(n);
t1 = performance.now();
console.log("range(n) Took " + (t1 - t0) + " milliseconds.");

t0 = performance.now();
arr = Array.from(rangeIter(0, n));
t1 = performance.now();
console.log("Array.from(rangeIter(0, n)) Took " + (t1 - t0) + " milliseconds.");

t0 = performance.now();
arr = [...rangeIter(0, n)];
t1 = performance.now();
console.log("[...rangeIter(0, n)] Took " + (t1 - t0) + " milliseconds.");

t0 = performance.now();
arr = Array.from(sequence(n));
t1 = performance.now();
console.log("Array.from(sequence(n)) Took " + (t1 - t0) + " milliseconds.");

t0 = performance.now();
arr = [...sequence(n)];
t1 = performance.now();
console.log("[...sequence(n)] Took " + (t1 - t0) + " milliseconds.");

t0 = performance.now();
arr = Array(n).fill(0).map(Number.call, Number);
t1 = performance.now();
console.log("Array(n).fill(0).map(Number.call, Number) Took " + (t1 - t0) + " milliseconds.");

t0 = performance.now();
arr = Array.from(Array(n).keys());
t1 = performance.now();
console.log("Array.from(Array(n).keys()) Took " + (t1 - t0) + " milliseconds.");

t0 = performance.now();
arr = [...Array(n).keys()];
t1 = performance.now();
console.log("[...Array(n).keys()] Took " + (t1 - t0) + " milliseconds.");

最快的是Array(n).fill(0).map(Number.call,Number),第二个是[…Array(n).keys()]

但是。。。rangeIter的方式非常方便(可以内联),速度快,功能更强大

其他回答

只是另一个ES6版本。

通过使用Array.from第二个可选参数:

Array.from(arrayLike[,mapFn[,thisArg]])

我们可以从空数组(10)位置构建编号数组:

Array.from(Array(10), (_, i) => i)

var arr=数组.来自(数组(10),(_,i)=>i);文档.写入(arr);

可以使用函数生成器或函数*表达式。这里是[https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/function]以及对函数生成器链接的引用[https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function].

设a=1,b=10;

函数*范围(a,b){对于(var i=a;i<=b;++i)收益率i;}

数组.来自(范围(a,b));// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

[…范围(a,b)]// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for(var i,a=[i=0];i<10;a[i++]=i);

a=[1、2、3、4、5、6、7、8、9、10]

在ES6中,使用Array from()和key()方法。

Array.from(Array(10).keys())
//=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

使用排列运算符的较短版本。

[...Array(10).keys()]
//=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

从1开始,将map函数传递给Array from(),对象具有长度属性:

Array.from({length: 10}, (_, i) => i + 1)
//=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

在ES6中,您可以执行以下操作:

数组(N).fill().map((e,i)=>i+1);

http://jsbin.com/molabiluwa/edit?js安慰

编辑:更新问题后,将数组(45)更改为数组(N)。

控制台日志(数组(45).填充(0).映射((e,i)=>i+1));