Tersest way to create an array of integers from 1.

2020-01-27 02:37发布

What would be the tersest way to create this array:

var x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
         11, 12, 13, 14, 15, 16, 17, 18, 19, 20];

For example, a for loop:

var x = [];
for (var i=1;i<=20;i++) {
  x.push(i);
}

Or a while loop:

var x = [], i = 1, endInt = 20;
while (i <= endInt) {
  x.push(i);
  i++;
}

Would there be other examples that would be terser -- in other words -- less code? I'm thinking of things like in Ruby where the equivalent code I believe would be as simple as 1..20. I'm not aware of syntax like that in JavaScript but I'm wondering if there are shorter ways to do that same thing.

UPDATE: I wasn't thinking of removing semicolons or var for answers in the question, but I have to admit the question implies that. I am more curious about algorithms than shaving bytes. Sorry if I was unclear! Also, making it into a function is simple enough, just slap function range(start, end) { /* guts here */ } around it and you're there. The question is are there novel approaches to the "guts."

12条回答
叼着烟拽天下
2楼-- · 2020-01-27 03:03
var i = 0;
var x = [];
while (i++ < 20) x.push(i);

JSFiddle

查看更多
够拽才男人
3楼-- · 2020-01-27 03:04

It can be done with features from the ES6, currently only supported by Firefox thou. I found a compatibility table here: http://kangax.github.io/compat-table/es6/

Array.from(new Array(20), (x,i) => i+1)

If you want to have some other range then I guess you could do

Array.from(new Array(5), (x,i) => i+5)

Which would then be [5,6,7,8,9]

查看更多
虎瘦雄心在
4楼-- · 2020-01-27 03:04

while-- is the way to go

var a=[],b=10;while(b--)a[b]=b+1

returns [1,2,3,4,5,6,7,8,9,10]

explained with start & length

var array=[],length=20,start=5;while(length--)array[length]=length+start

returns [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]

want range?

explained with start & end

var array=[],end=30,start=25,a=end-start+1;while(a--)array[a]=end--

returns [25, 26, 27, 28, 29, 30]

for --

for(var a=[],b=20;b>0;b--,a[b]=b+1)

for++

for(var a=[],b=0;b<20;b++,a[b]=b+1)

WHY is this theway to go?

  1. while -- is prolly the fastest loop;

  2. direct setting is faster than push & concat;

  3. [] is also faster than new Array(10);

  4. it's not much longer code than all the others

byte saving techniques:

  1. use the arguments as a placholder forthe in function variables
  2. don't use new Array(),push(),concat() if not needed
  3. place "(){};," only when needed.
  4. use a,b,c,d... in short functions.

so if u want a function for this

with start,end (range)

function range(a,b,c,d){d=[];c=b-a+1;while(c--)d[c]=b--;return d}

so now range(3,7) returns [3,4,5,6,7]

u save bytes in many ways here and this function is also very fast as it does not use concat, push, new Array and it's made with a while --

查看更多
5楼-- · 2020-01-27 03:06

If you are looking to shave characters off anyway possible without regard for readability, this is the best I can do:

var x=[],i=0
while(i<20)
  x[i]=i+++1

Not a lot better than yours though.

Edit:

Actually this works better and shaves off a couple characters:

var x=[],i=0
while(i<20)
  x[i]=++i

Edit 2:

And here's my entry for a general "range" function in the least number of characters:

function range(s,e){var x=[];while(s<e+1)x.push(s++);return x}

Again, don't write code this way. :)

查看更多
不美不萌又怎样
6楼-- · 2020-01-27 03:12

In my knowledge, the option of using for loop, as you mentioned, is the most tersest.

That is,

var x = [];
for (var i=1;i<=20;i++) {
  x.push(i);
}
查看更多
爷、活的狠高调
7楼-- · 2020-01-27 03:13

Using ES6

numArr = Array(5).fill(0).reduce(arr=>{ arr.push(arr.length); return arr },[])

查看更多
登录 后发表回答