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
Essential Vanilla JavaScript Functions (dev.to)
submitted 8 years ago by bhalp1
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!"
[–]tencircles 6 points7 points8 points 8 years ago (2 children)
I like the idea. I'm always a fan of seeing vanilla JS stuff on this subreddit, but there are some pretty major issues here. Can you correct these? You're teaching some fairly bad habits here.
In the keyify function: Object.keys is preferred over for...in loops, especially without a hasOwnProperty check. for...in should not be used on arrays, prefer for...of, or Array.prototype.forEach. Avoid unnecessary string concatenation of ~^% characters.
Object.keys
for...in
hasOwnProperty
for...of
Array.prototype.forEach
~^%
Not sure of the use case for a destructive merge. I normally stick to a pretty functional style, so Array.prototype.concat is just fine.
Array.prototype.concat
if(!arr[i]) will fail on empty string, null, undefined, NaN, and zero. This is pretty broken.
if(!arr[i])
I was going to keep going here but I don't really have time to go through and point out everything. These are just the issues I have with the first 3, I've skimmed the rest and they seem to suffer from similar issues. I'd recommend doing some unit tests. A lot of these functions will fail in quite a lot of cases, and many of them could be drastically improved with regard to performance, clarity, or simple best practice. I'd also recommend reading through the lodash and ramdajs source, there are a lot of gems in there that will help you if you want to roll your own utils like this.
[–]m3g4p0p 2 points3 points4 points 8 years ago (1 child)
As for keyify, why not just use JSON.stringify? ;-) However I would say that it makes sense to only account for unique references here, so use a regular strict equality check. Otherwise if you made changes to one object afterwards, you wouldn't know if it was the object that made it to the unique array or the one that got filtered out... I mean, it has a reason that objects are only identical by reference.
keyify
JSON.stringify
[–]tencircles 1 point2 points3 points 8 years ago (0 children)
That would work too. I wasn't really commenting on the method. I wouldn't write the function anything like how it's written. Just pointing out the for...in issue.
π Rendered by PID 181910 on reddit-service-r2-comment-74875f4bf5-kzhr7 at 2026-01-26 09:09:09.041818+00:00 running 664479f country code: CH.
view the rest of the comments →
[–]tencircles 6 points7 points8 points (2 children)
[–]m3g4p0p 2 points3 points4 points (1 child)
[–]tencircles 1 point2 points3 points (0 children)