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

var foo = [];

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

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


当前回答

使用递归的ES6解决方案。不同于所有其他解决方案

const range = (n, A = []) => (n === 1) ? [n, ...A] : range(n - 1, [n, ...A]);


console.log(range(5));

其他回答

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 iterators
class 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);

//不分配N大小数组(ES6,带有一些流注释)的解决方案:函数*zeroToN(N/*:数字*/)/*:生成器<number,void,empty>*/{对于(设n=0;n<=n;n+=1),得到n;}//通过这一代,您可以拥有您的阵列console.log([…zeroToN(10-1)])//但是让我们定义一个助手迭代器函数函数mapIterator(迭代器,映射){常量arr=[];for(let result=iterater.next()!result.done;result=iterator.next()){arr.push(映射(result.value));}返回arr;}//现在您有了一个map函数,不需要分配0…N-1数组console.log(mapIterator(zeroToN(10-1),n=>n*n));

可以使用Int8Array、Int16Array和Int32Array创建范围从1到n的数组,如下所示:

const zeroTo100 = new Int8Array(100).map((curr, index) => curr = index + 1);
/* Int8Array(100) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 
36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 
55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 
74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 
93, 94, 95, 96, 97, 98, 99, 100]

还可以使用以下类型化数组在数组中生成1到n个项。

Uint8Array、Uint16Array和Uint32ArrayBigInt64阵列Uint8约束阵列浮置阵列32、浮置阵列64

当然,除了数字之外,您无法在这些数组中放置任何东西,所以使用这个小快捷方式会带来风险。

此外,如果您只需要一个包含n个零的数组,那么只需执行以下操作:

const arr_100_0s = new Int8Array(100)

编辑:您可以使用它快速生成范围,如下所示:

function range(start, end) {
    const arr = new Int8Array(end - start + 1).map((curr, i) => curr + i + start);
    return arr;
}

range(15, 30); // Int8Array(16) [15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]

不完全符合用户的要求,但与IMO高度相关。

表演

今天2020.12.11我在Chrome v87、Safari v13.1.2和Firefox v83上对macOS HighSierra 10.13.6进行了测试,以确定所选的解决方案。

后果

适用于所有浏览器

解决方案O(基于while)是最快的(除了Firefox for big N-但在那里很快)解决方案T在Firefox上最快解M,P对于小N很快溶液V(lodash)对于大N是快速的对于小N,溶液W、X是缓慢的溶液F缓慢

细节

我执行两个测试用例:

对于小N=10-您可以在这里运行对于大N=1000000,您可以在此处运行

下面的片段展示了所有测试的解决方案ABCDEFGH我JKLMNOPQRSTU五、W十、

函数A(N){return Array.from({length:N},(_,i)=>i+1)}函数B(N){return Array(N).fill().map((_,i)=>i+1);}函数C(N){return Array(N).jjoin().split(',').map((_,i)=>i+1);}函数D(N){return Array.from(数组(N),(_,i)=>i+1)}函数E(N){return Array.from({length:N},(_,i)=>i+1)}函数F(N){return Array.from({length:N},Number.call,i=>i+1)}函数G(N){return(数组(N)+“”).split(',').map((_,i)=>i+1)}函数H(N){return[…Array(N).keys()].map(i=>i+1);}函数I(N){return[…Array(N).keys()].map(x=>x+1);}函数J(N){return[…数组(N+1).keys()].sslice(1)}函数K(N){return[…Array(N).keys()].map(x=>++x);}函数L(N){let arr;(arr=[…数组(N+1).keys()]).shift();返回arr;}函数M(N){var arr=[];变量i=0;而(N-)arr.push(++i);返回arr;}函数N(N){变量a=[],b=N;而(b-)a[b]=b+1;返回a;}函数O(N){var a=阵列(N),b=0;而(b<N)a[b++]=b;返回a;}函数P(N){var foo=[];对于(var i=1;i<=N;i++)foo.push(i);返回foo;}函数Q(N){对于(var a=[],b=N;b-;a[b]=b+1);返回a;}函数R(N){对于(变量i,a=[i=0];i<N;a[i++]=i);返回a;}函数S(N){设foo,x;对于(foo=[x=N];x;foo[x-1]=x--);返回foo;}函数T(N){返回新的Uint8Array(N).map((item,i)=>i+1);}函数U(N){返回“_”。重复(5)。拆分(“”)。映射((_,i)=>i+1);}函数V(N){返回范围(1,N+1);}函数W(N){return[…(函数*(){让i=0;while(i<N)yield++i})()]}函数X(N){函数序列(最大值,步长=1){返回{[Symbol.iiterat]:函数*(){对于(设i=1;i<=max;i+=步长),得出i}}}返回[…序列(N)];}[A、B、C、D、E、F、G、H、I、J、K、L、M、N、O、P、Q、R、S、T、U、V、W、X]。对于每个(F=>{console.log(`${f.name}${f(5)}`);})<script src=“https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js“integrity=”sha512-90vH1Z83AJY9DmlWa8WkjkV79yfS2n2Oxhsi2dZbIv0nC4E6m5AbH8Nh156kkM7JePmqD6tcZsfad1ueoaovww==“crossrorigin=”匿名“></script>此代码段仅显示性能测试中使用的函数-它本身不执行测试!

下面是铬的示例结果

使用不修改Number.prototype的生成器函数的可移植版本。

函数序列(最大值,步长=1){返回{[Symbol.iiterat]:函数*(){对于(设i=1;i<=max;i+=步长),得出i}}}console.log([…序列(10)])