如何只在ES2015中生成0到n的范围?

时间:2022-05-23 22:52:02

I have always found the range function missing from JavaScript as it is available in python and others? Is there any concise way to generate range of numbers in ES2015 ?

我总是发现JavaScript中缺少range函数,因为它在python和其他语言中是可用的。在2015年的2015年,是否有任何简洁的方式来产生一系列的数字?

EDIT: MY question is different from the mentioned duplicate as it is specific to ES2015 and not ECMASCRIPT-5. Also I need the range to be starting from 0 and not specific starting number (though it would be good if that is there)

编辑:我的问题不同于上面提到的副本,因为它是针对ES2015而不是ECMASCRIPT-5的。我还需要范围从0开始而不是特定的起始数(如果有的话那就更好了)

7 个解决方案

#1


118  

You can use the spread operator on the keys of a freshly created array.

您可以在新创建的数组的键上使用扩展操作符。

[...Array(n).keys()]

[…]数组(n). keys())

or

Array.from(Array(n).keys())

Array.from(数组(n). keys())

The Array.from() syntax is necessary if working with TypeScript

如果使用打字稿,则需要使用Array.from()语法

#2


65  

I also found one more intuitive way using Array.from:

我还发现了一种更直观的使用array的方法。

const range = n => Array.from({length: n}, (value, key) => key)

Now this range function will return all the numbers starting from 0 to n-1

现在这个范围函数将返回从0到n-1的所有数字

A modified version of the range to support start and end is:

支持开始和结束的范围的修改版本是:

const range = (start, end) => Array.from({length: (end - start)}, (v, k) => k + start);

EDIT As suggested by @marco6, you can put this as a static method if it suits your use case

按照@marco6的建议进行编辑,如果它适合您的用例,您可以将其作为静态方法

Array.range = (start, end) => Array.from({length: (end - start)}, (v, k) => k + start);

and use it as

和使用它

Array.range(3, 9)

#3


3  

With Delta

与δ

For javascript

对javascript

Array.from(Array(10).keys()).map((v,i)=> 4 + i * 2);
//=> [4, 6, 8, 10, 12, 14, 16, 18, 20, 22]

Array.from(Array(10).keys()).map((v,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(0).map((v, i) => 4 + i * -2);
//=> [4, 2, 0, -2, -4, -6, -8, -10, -12, -14]

const range = (from, to, step)=> 
    Array(Math.floor((to - from) / step) + 1)
    .fill(0)
    .map((v, i) => from + i * step);

range(0, 9, 2);
//=> [0, 2, 4, 6, 8]

Array.range = (from, to, step) => Array.from(
    { length: Math.floor((to - from) / step) + 1 }, 
    (v, k) => from + k * 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]

For Typescript

为打印稿

interface _Iterable extends Iterable<{}> {
    length: number;
}

class _Array<T> extends Array<T> {
    static range(from: number, to: number, step: number): number[] {
        return Array.from(
            (<_Iterable>{ length: Math.floor((to - from) / step) + 1 }),
            (v, k) => from + k * step
        );
    }
}
_Array.range(0, 9, 1);
//=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

#4


2  

To support delta

支持δ

const range = (start, end, delta) => {
  return Array.from(
    {length: (end - start) / delta}, (v, k) => (k * delta) + start
  )
};

#5


1  

You can also do it with a one liner with step support like this one:

你也可以用一个带有阶梯支撑的衬垫来做:

((from, to, step) => ((add, arr, v) => add(arr, v, add))((arr, v, add) => v < to ? add(arr.concat([v]), v + step, add) : arr, [], from))(0, 10, 1)

((,,一步)= >((添加、arr v)= > add(加勒比海盗,v,添加))((加勒比海盗,v,添加)= > v < ?add(arr.concat([v]),v +步骤,添加):加勒比海盗,[],从))(0,10日1)

The result is [0, 1, 2, 3, 4, 5, 6 ,7 ,8 ,9].

结果是[0,1,2,3,4,5,6,7,8,9]。

#6


0  

const keys = Array(n).keys();
[...Array.from(keys)].forEach(callback);

in Typescript

在打印稿

#7


0  

Here's another variation that doesn't use Array.

这是另一个不使用数组的变体。

let range = (n, l=[], delta=1) => {
  if (n < 0) { 
    return l 
  }
  else {
    l.unshift(n)
    return range(n - delta, l) 
  }
}

#1


118  

You can use the spread operator on the keys of a freshly created array.

您可以在新创建的数组的键上使用扩展操作符。

[...Array(n).keys()]

[…]数组(n). keys())

or

Array.from(Array(n).keys())

Array.from(数组(n). keys())

The Array.from() syntax is necessary if working with TypeScript

如果使用打字稿,则需要使用Array.from()语法

#2


65  

I also found one more intuitive way using Array.from:

我还发现了一种更直观的使用array的方法。

const range = n => Array.from({length: n}, (value, key) => key)

Now this range function will return all the numbers starting from 0 to n-1

现在这个范围函数将返回从0到n-1的所有数字

A modified version of the range to support start and end is:

支持开始和结束的范围的修改版本是:

const range = (start, end) => Array.from({length: (end - start)}, (v, k) => k + start);

EDIT As suggested by @marco6, you can put this as a static method if it suits your use case

按照@marco6的建议进行编辑,如果它适合您的用例,您可以将其作为静态方法

Array.range = (start, end) => Array.from({length: (end - start)}, (v, k) => k + start);

and use it as

和使用它

Array.range(3, 9)

#3


3  

With Delta

与δ

For javascript

对javascript

Array.from(Array(10).keys()).map((v,i)=> 4 + i * 2);
//=> [4, 6, 8, 10, 12, 14, 16, 18, 20, 22]

Array.from(Array(10).keys()).map((v,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(0).map((v, i) => 4 + i * -2);
//=> [4, 2, 0, -2, -4, -6, -8, -10, -12, -14]

const range = (from, to, step)=> 
    Array(Math.floor((to - from) / step) + 1)
    .fill(0)
    .map((v, i) => from + i * step);

range(0, 9, 2);
//=> [0, 2, 4, 6, 8]

Array.range = (from, to, step) => Array.from(
    { length: Math.floor((to - from) / step) + 1 }, 
    (v, k) => from + k * 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]

For Typescript

为打印稿

interface _Iterable extends Iterable<{}> {
    length: number;
}

class _Array<T> extends Array<T> {
    static range(from: number, to: number, step: number): number[] {
        return Array.from(
            (<_Iterable>{ length: Math.floor((to - from) / step) + 1 }),
            (v, k) => from + k * step
        );
    }
}
_Array.range(0, 9, 1);
//=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

#4


2  

To support delta

支持δ

const range = (start, end, delta) => {
  return Array.from(
    {length: (end - start) / delta}, (v, k) => (k * delta) + start
  )
};

#5


1  

You can also do it with a one liner with step support like this one:

你也可以用一个带有阶梯支撑的衬垫来做:

((from, to, step) => ((add, arr, v) => add(arr, v, add))((arr, v, add) => v < to ? add(arr.concat([v]), v + step, add) : arr, [], from))(0, 10, 1)

((,,一步)= >((添加、arr v)= > add(加勒比海盗,v,添加))((加勒比海盗,v,添加)= > v < ?add(arr.concat([v]),v +步骤,添加):加勒比海盗,[],从))(0,10日1)

The result is [0, 1, 2, 3, 4, 5, 6 ,7 ,8 ,9].

结果是[0,1,2,3,4,5,6,7,8,9]。

#6


0  

const keys = Array(n).keys();
[...Array.from(keys)].forEach(callback);

in Typescript

在打印稿

#7


0  

Here's another variation that doesn't use Array.

这是另一个不使用数组的变体。

let range = (n, l=[], delta=1) => {
  if (n < 0) { 
    return l 
  }
  else {
    l.unshift(n)
    return range(n - delta, l) 
  }
}