Essential Vanilla JavaScript Functions
Some of the missing functions in JavaScript in vanilla form (inspired by PHP). You can just try ...
For further actions, you may consider blocking this person and/or reporting abuse
Interesting approach for array_unique (even taking into account that objects and arrays in the array might have different pointers, but have the same content. There's just one minor issue:
x in y
will also return true for the constructor attribute, so better usey.hasOwnProperty(x)
. If your array contains'constructor'
as a string, your function will filter it out.Yeah. We can implement it that way as well. If you want you can send over a pull request on github.com/amitmerchant1990/essent...
There you go.
Sorry, but while the big majority of these functions are pretty useless (you might find them in lodash or even in the language itself), the implementations show a big misunderstanding of how JavaScript works.
You can do pretty much anything with Array functions like
map
,reduce
,filter
,slice
andconcat
. They are helpful because they do not mutate the original array, but produce a new one.And finally, it is really not necessary to pollute your code with such ugly names, even if PHP provides them. Next readers won't necessary know a lot about PHP.
This, and you can even use the ...spread operator instead of concat, to create a new array from an existing one.
The above functions are translated a bit too literally from PHP, or perhaps the intention was to make them work for ES5 / lower?
Why are
map
,reject
andtaken
part of your list (they already exist in vanilla JS)? When (besides an job interview) did you have the need forstrrev
?Besides that a very nice exercise.
Pardon me for that. I was curious and was just trying to do my implementation.
Gotta give a shout out to Locutus here, a source for hundreds of Javascript implementations of PHP functions (and other languages).
I love what you have done. Keep it up. The raised point on mutation is a good one. These need to process cloned objects not original. With regard these (map, reduce, filter, slice and concat), blind use of these is as good as blind use of frameworks.
Neat! I love doing things in Vanilla JavaScript over including a big library whenever I can. However, to make the code a bit nicer I'd suggest adding the array and string methods to their actual object prototypes if possible. In my opinion it makes the code look much nicer and is a convenience Javascript has the original PHP functions do not. (That's the reason PHP has so many functions that start with "array" or "string")
Awesome utilities, just one question, what is this line for?
ret += "~" + j + "^" + keyify(obj[j]) + "%";
Why do you use "~", ^ and "%"?
Yes! This is exactly what I needed. As a PHP developer I always use those functions. Thank you.
Fun. Thanks!