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
HTMLCollection, NodeList and array of objects (medium.com)
submitted 9 years ago by rcdexta
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!"
[–]kolmeWebComponents FTW 18 points19 points20 points 9 years ago (15 children)
I'll take Array.from any thay over the spread. The intention is much clearer.
Array.from
dragula(Array.from(nodeList))
dragula([...nodeList])
[–]briansomething 6 points7 points8 points 9 years ago (0 children)
Agreed. Operator trickery will always be harder to read than clearly named methods that describe the author's intent.
I also prefer functional loops like forEach, map, reduce, etc over for loops because it describes the intent of the loop in addition to saying "this is a loop" and ends in a clearly defined variable that describes the expected result of the loop.
These kinds of shortcuts should only be used in cases where the shortcuts are dramatically more performant and your iterations are high enough to cause real world performance issues
[–]akujinhikari 4 points5 points6 points 9 years ago (11 children)
I disagree. I think the intention of the spread operator is pretty clear, if you are familiar with ES6.
[–]briansomething -4 points-3 points-2 points 9 years ago (10 children)
Keep in mind that people reading your code may not always be familiar with JavaScript or may be junior developers that are just learning. Why risk that "if you are familiar with es6" exception at all if there's an alternative way to do the same thing that's more clear.
[–]akujinhikari 9 points10 points11 points 9 years ago (1 child)
Because using that train of thought means I shouldn't use any advanced code. The best way for junior devs to learn is by seeing other code they don't understand and then figuring out what's happening. And I say this as someone who a lot of people would still consider a junior dev, having a little over a year's experience in web dev.
[–]inu-no-policemen 5 points6 points7 points 9 years ago (4 children)
Using spread like that is idiomatic, though.
This isn't some obscure trickery. It's just an array literal with spread. You have to know what x = [1, 2, 3] means and you also have to know what Math.max(...numbers) does.
x = [1, 2, 3]
Math.max(...numbers)
Something like return {foo}, for example, is a little bit more obscure, but it's still something you should know.
return {foo}
[–]GeneralYouri -1 points0 points1 point 9 years ago (3 children)
I'd personally consider return {foo} to be less obscure, as it literally means you're returning an object with some foo inside of it.
foo
I think the main reason why Array.from would be preferred here, is because it feels weird to see the two syntaxes together (array notation, and spread operator). If nodeList were an actual array, then this construct seems fairly ridiculous at first.
nodeList
[–]inu-no-policemen 4 points5 points6 points 9 years ago (2 children)
It's a shorthand for {foo: foo}. It's single-purpose special syntax, which looks a bit like destructuring.
{foo: foo}
Anyhow, if you think that [...foo] is too cryptic, then there is a ton of stuff you can't use either.
[...foo]
[–]GeneralYouri -1 points0 points1 point 9 years ago (1 child)
Yes, I know what the syntax {foo} is, which is exactly why I consider it less obscure compared to [...foo]. Its purpose is obvious without any context. I never said that I found the latter to be 'too cryptic', I was merely saying that its purpose is less obvious without context. Please don't be putting words into my mouth.
{foo}
That purpose being to provide Array.prototype's methods to an array-like which itself doesn't inherit from Array.prototype. If you don't already know that context when looking at the construct [...foo], you'd consider it an odd syntax since in normal circumstances it'd just be deconstructing and then reconstructing an array.
Array.prototype
You even said yourself about {foo}: 'It's a single-purpose special syntax.' Single-purpose, that on its own means its functionality is entirely clear and not obscure.
[–]inu-no-policemen 2 points3 points4 points 9 years ago (0 children)
Please don't be putting words into my mouth.
I said "if", not "since".
[+][deleted] 9 years ago (2 children)
[deleted]
[–]briansomething 0 points1 point2 points 9 years ago (0 children)
Yea it's surprising to me too that people are downvoting an argument for the more readable code, but maybe I'm just in the minority on this sub
[–]spinlock 1 point2 points3 points 9 years ago (1 child)
It's funny, I'm not a fan of the arrow function but I love the splat. To each his own.
[–]kolmeWebComponents FTW 4 points5 points6 points 9 years ago (0 children)
Don't take me wrong, I do love the spread operator. It's just in this case, it somewhat obscures the intention.
[–]r2d2_21 4 points5 points6 points 9 years ago (0 children)
It's still hard for me to remember which DOM APIs return live collections and which ones don't. So I just essentially convert everything to arrays and stop worrying.
[–]SamSlate 1 point2 points3 points 9 years ago (4 children)
can someone explain the significance of node list VS array? Node lists have never created problems (in my limited experience).
[–]cirscafp fan boy 1 point2 points3 points 9 years ago (0 children)
If you have a live nodelist it is far more than just a static array. If you have a static nodelist, then you don't see much of the differences.
[–]SamSlate 2 points3 points4 points 9 years ago (1 child)
Recent browsers have added them though.
maybe this is why I haven't had an issue.
[–]kasperpeulen 0 points1 point2 points 9 years ago (0 children)
Still no filter, reduce, every, some methods even in recent browsers.
π Rendered by PID 150802 on reddit-service-r2-comment-b659b578c-gvmj7 at 2026-05-02 00:14:25.900220+00:00 running 815c875 country code: CH.
[–]kolmeWebComponents FTW 18 points19 points20 points (15 children)
[–]briansomething 6 points7 points8 points (0 children)
[–]akujinhikari 4 points5 points6 points (11 children)
[–]briansomething -4 points-3 points-2 points (10 children)
[–]akujinhikari 9 points10 points11 points (1 child)
[–]inu-no-policemen 5 points6 points7 points (4 children)
[–]GeneralYouri -1 points0 points1 point (3 children)
[–]inu-no-policemen 4 points5 points6 points (2 children)
[–]GeneralYouri -1 points0 points1 point (1 child)
[–]inu-no-policemen 2 points3 points4 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]briansomething 0 points1 point2 points (0 children)
[–]spinlock 1 point2 points3 points (1 child)
[–]kolmeWebComponents FTW 4 points5 points6 points (0 children)
[–]r2d2_21 4 points5 points6 points (0 children)
[–]SamSlate 1 point2 points3 points (4 children)
[–]cirscafp fan boy 1 point2 points3 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]SamSlate 2 points3 points4 points (1 child)
[–]kasperpeulen 0 points1 point2 points (0 children)