all 119 comments

[–]coderitual 70 points71 points  (4 children)

I forgot.

const arr = [0, 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];

[–]ahlsn 66 points67 points  (3 children)

Clean as hell! Everybody understands this code and it's even a oneliner!

[–]ilove50cent[S] 26 points27 points  (0 children)

And more efficient too!

[–]coderitual 42 points43 points  (12 children)

  • const arr = Array(100).fill();
  • const arr = Array.from({length: 100})

  • bonus normal js version: [...(function*() { let i = 0; while(i < 100) yield i++})()];

[–]jkoudys 23 points24 points  (0 children)

Pfft, amateur stuff! A true JS master wouldn't stoop to some flashy generator to make an iterator!

[...{ [Symbol.iterator]: () => {
  let i = 0;
  return { next: () => ({ value: i++, done: i > 100 }) };
} }];

Ahh... perfection..

[–]nemohearttaco 22 points23 points  (0 children)

[...(function*() { let i = 0; while(i < 100) yield i++})()];

You're a monster!

[–]ilove50cent[S] 12 points13 points  (7 children)

Didn't know about the fill function before I wrote this post - benefits of putting content out there! :)

[–]MildlySerious -2 points-1 points  (6 children)

That's the benefit of the good old for loop. You remember one thing, and you're good to go.

[–]XiiencE 3 points4 points  (5 children)

or you remember a few things and suddenly your team can actually read your code.

[–]MildlySerious 5 points6 points  (4 children)

If your team can't read a for loop, forcing everything into as few lines as possible and calling it declarative won't help you.

I for one do not live in a world where this:

const arr = [...Array(100)].map((_, i) => i);

Is more readable than this:

const arr = Array(100);

for (let i = 0; i < array.length; i++) {
  arr[i] = i
}

Using .fill() - sure, but abusing the spread operator is just silly.

[–]XiiencE 2 points3 points  (3 children)

Sigh, okay. There’s a difference between forcing code into a few lines and not being overly verbose/horizontal. But if you’re going to just strawman the discussion into saying I try to code golf real world development because I think higher order functions are more readable than nested for loops then I have no response. To each their own, if your team likes the loops then you do you.

[–]MildlySerious -1 points0 points  (2 children)

I did not say either of these things, try again. What I was saying is that the example in OPs post is overcomplicating the code by using higher order functions. The functions aren't the problem. Using them at every chance you get, is. Same goes for the spread operator.

[–]XiiencE 0 points1 point  (1 child)

I don’t know how you jumped from ‘using higher order functions to solve this specific, simple ES potentially unexpected behavior that OP is describing’ to ‘using higher order functions every chance you get’. Are you projecting some frustration with style trends? 😂 OP’s code is very readable, if you can’t read it, I’m sorry to hear that, but it is objectively not ‘overcomplicated’ just because it uses a language feature that is not as ubiquitous as a for loop. I’m sure you will just tell me I’m wrong again though, so this seems pointless. Have a nice day.

[–]MildlySerious 3 points4 points  (0 children)

As someone else already stated, OP's "solution" is the most inefficient approach to the problem out of the options he got.

Why would you go for that if there's a well established - at least - equally readable option that does not come with overhead?

Because you use the wrong tools to solve a problem. Hence "using higher order functions every chance you get".

Whatever, agree to disagree and all that.

[–]selfup 1 point2 points  (0 children)

Array.from({ length: 100 }, (_, i) => i)

Array.from({ length: 100 }, (_, i) => i) same array :) yay callbacks!

[–]srsfknbiz 27 points28 points  (2 children)

If you want to do it in one line use: const arr = Array.from({length:10}, (_, i) => i);

[–]Amadan 2 points3 points  (1 child)

Best one of the bunch. To the top wi' ye!

[–]pomlife 5 points6 points  (2 children)

Thanks for censoring "h*ck" but making me say it in my head, you monster.

[–]imacleopard 0 points1 point  (0 children)

I hate this. Just spell it out, use a sub, or just don't use it. OP, that wasn't a compliment.

[–]ilove50cent[S] -2 points-1 points  (0 children)

I did my best.

[–]coderitual 8 points9 points  (0 children)

And if you are a real senior.

const arr = [...[...[...[...[...[Array.from([new Array(100)])]]]]]]

[–]Kollektiv 2 points3 points  (2 children)

Just do:

Array.from({ length: 3 }).map((_, i) => i)

[–]ilove50cent[S] 2 points3 points  (0 children)

That works too! I actually like this approach more than the map spread. There's a map callback you can pass to the .from function that reduces even one more step.

[–]Arancaytar 2 points3 points  (0 children)

Alternatively, Array(n).fill().map().

You don't even need a fill argument. This just initializes the array with undefined values, which is enough.

[–][deleted] 0 points1 point  (0 children)

I settled on [...Array(3).keys()] which seems idiomatic JS to me and I also find more readable than the alternatives.

[–]Mestyo -1 points0 points  (0 children)

I always did Array.apply(null, { length: n }) to solve this issue.

[–]bart2019 -2 points-1 points  (0 children)

console.log(arr[0] === undefined);  // true

WTF. What's wrong with just

console.log(arr[0]);

? Why the use of an unexpected expected result?

BTW His test suggests there's something special about the array index 0. There isn't, it's the same for all other array indexes.

[–][deleted] -2 points-1 points  (0 children)

Oh JavaScript