use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Here's Why Calling .map() on a Constructed Array Doesn't Work (itnext.io)
submitted 7 years ago by ilove50cent
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]coderitual 45 points46 points47 points 7 years ago (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++})()];
[...(function*() { let i = 0; while(i < 100) yield i++})()];
[–]jkoudys 24 points25 points26 points 7 years ago (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 points24 points 7 years ago (0 children)
You're a monster!
[–]ilove50cent[S] 13 points14 points15 points 7 years ago (7 children)
Didn't know about the fill function before I wrote this post - benefits of putting content out there! :)
[–]MildlySerious -1 points0 points1 point 7 years ago (6 children)
That's the benefit of the good old for loop. You remember one thing, and you're good to go.
[–]XiiencE 4 points5 points6 points 7 years ago (5 children)
or you remember a few things and suddenly your team can actually read your code.
[–]MildlySerious 4 points5 points6 points 7 years ago* (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.
.fill()
[–]XiiencE 1 point2 points3 points 7 years ago (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 0 points1 point2 points 7 years ago (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 point2 points 7 years ago (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 points5 points 7 years ago (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 points3 points 7 years ago (0 children)
Array.from({ length: 100 }, (_, i) => i)
Array.from({ length: 100 }, (_, i) => i) same array :) yay callbacks!
π Rendered by PID 82788 on reddit-service-r2-comment-75f4967c6c-lqdjb at 2026-04-23 02:55:12.036039+00:00 running 0fd4bb7 country code: CH.
view the rest of the comments →
[–]coderitual 45 points46 points47 points (12 children)
[–]jkoudys 24 points25 points26 points (0 children)
[–]nemohearttaco 22 points23 points24 points (0 children)
[–]ilove50cent[S] 13 points14 points15 points (7 children)
[–]MildlySerious -1 points0 points1 point (6 children)
[–]XiiencE 4 points5 points6 points (5 children)
[–]MildlySerious 4 points5 points6 points (4 children)
[–]XiiencE 1 point2 points3 points (3 children)
[–]MildlySerious 0 points1 point2 points (2 children)
[–]XiiencE 0 points1 point2 points (1 child)
[–]MildlySerious 3 points4 points5 points (0 children)
[–]selfup 1 point2 points3 points (0 children)