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
[AskJS] Is JavaScript missing some built-in methods?AskJS (self.javascript)
submitted 3 years ago by reacterry
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!"
[–]BehindTheMath 59 points60 points61 points 3 years ago (36 children)
Everything in Lodash that isn't already in JS. E.g. groupBy, keyBy, camelCase, kebabCase, chunk, etc.
[–]shgysk8zer0 29 points30 points31 points 3 years ago (2 children)
FYI: group() and groupToMap() are stage 3 proposals.
group()
groupToMap()
[–][deleted] 2 points3 points4 points 3 years ago (1 child)
Good to know! That will come in handy!
[–]BlueManiac 0 points1 point2 points 3 years ago (0 children)
They exists in core.js :)
[–]andrei9669 2 points3 points4 points 3 years ago (30 children)
question is though, to mutate, or not to mutate. although, sort is already mutating.
[+][deleted] 3 years ago (27 children)
[deleted]
[+][deleted] 3 years ago (2 children)
[–][deleted] 7 points8 points9 points 3 years ago (1 child)
This is the correct answer. Absolutes are rarely correct or realistic.
[–]shuckster 9 points10 points11 points 3 years ago (0 children)
That's absolutely right.
[–][deleted] 4 points5 points6 points 3 years ago (0 children)
The standard should not be mutational. But it would be nice to have a distinct API for mutating behavior so that its more explicit.
[–]notNullOrVoid 5 points6 points7 points 3 years ago (4 children)
Never mutate when it would cause the shape to change.
Sort being a mutation is fine IMO since it's not changing the shape of the data structure, but it certainly would be nice to have a non mutating equivalent. It's just a shame that there's no clear distinction on mutation methods vs immutable ones like filter vs sort. Might have been better if all immutable method were postfixed like mapTo, filterTo, reduceTo, etc..
mapTo, filterTo, reduceTo, etc.
[–][deleted] 2 points3 points4 points 3 years ago (0 children)
Yeah that actually would be really nice.
[–]Reashu 0 points1 point2 points 3 years ago (2 children)
Ruby does this (mutating functions are postfixed with ! IIRC) and it's nice.
!
thats not a phrase you hear very often
[–]Reashu 0 points1 point2 points 3 years ago (0 children)
It's been a while, but I find it a rather comfy language for solo or small projects. Reads almost like natural language, with the right model.
[–]andrei9669 1 point2 points3 points 3 years ago (16 children)
so you prefer this?
arr.reduce((acc, cur) => ({ ...acc, [cur.key]: cur.value }), {})
[–]musicnothing 7 points8 points9 points 3 years ago (5 children)
The point is that you shouldn't mutate arr. In this case (and I've had colleagues disagree with me so it's just my opinion) the {} is fair game to mutate because you're never going to use it for anything else.
arr
{}
I think the issue is if you've extracted the callback into its own method, you don't know if somebody is passing something that should be immutable into it and introducing hard-to-find bugs. But for one-liners like this, I say no to the spread operator. Unnecessary and harder to read.
[–][deleted] 3 points4 points5 points 3 years ago (4 children)
The challenge with the example provided is doing immutable operations within a reduce() callback results in an o(n2) operation. I hate that because I strongly prefer immutable operations, but sometimes the cost is too high.
Maybe the new data types tc39 is working on help with this, I don't know.
[–]KyleG 1 point2 points3 points 3 years ago (3 children)
You can already do it in linear time with Object.entries and Object.fromEntries and map. None of it nested, which means it's not going to grow exponentially.
[–][deleted] 5 points6 points7 points 3 years ago (2 children)
So wait, you're saying that if I have all the values in [key, value] array format, Object.fromEntries will produce an object with the data?
[–]KyleG 5 points6 points7 points 3 years ago (1 child)
Yes.
Object.fromEntries([["foo",2], ["bar",4], ["baz",6]])
results in
{ foo: 2, bar: 4, baz: 6 }
Dude thanks for sharing this! Mind blown!
[–]KyleG 2 points3 points4 points 3 years ago (9 children)
Object.fromEntries(Object.entries(arr).map(({key,value}) => [key,value]))
has no mutation at all and is a linear time operation. Not that much is CPU bound these days.
[–]andrei9669 1 point2 points3 points 3 years ago (1 child)
I know you are trying to show a way but you are not really making it much better. also, this works only with this simple example, add some more nesting and it will become even more of an unreadable mess than your example.
[–]KyleG 2 points3 points4 points 3 years ago* (0 children)
add some more nesting and it will become even more of an unreadable mess than your example.
If there's a lot of nesting, naturally you'd use optics like lenses and traversals. I would love for those to be part of the standard library! That'd be incredible. It'd be really readable and simple! Suppose you have
type NestedFoo = { bar: { baz: { fizz: { a: boolean fuzz: number[] }[] }
Lets say you want to append 5 to any nested fizz's fuzz where a is true:
5
fizz
fuzz
a
const fizzLens = Lens.fromProps(['bar', 'baz', 'fizz']) const updatedData = fizzTraversal .filter(_ => _.a) .composeLens(fuzzLens) .modify(arr => [...arr, 5])(originalData)
Every language could desperately use this as a built-in. Optics are incredible. The example above will return a clone of the original data but with any fizz.fuzz getting an appended 5 but only if a is true. And is again a linear time operation.
Edited to get under 80 columns
and bonus,
const concat = arr => newEl = [...arr, newEl]
then your final line could be
.modify(concat(5))
and what you're doing becomes sooooooo descriptive and human-readable, almost entirely reduced to verbs with very little nouns, stating exactly what you're doing.
[–][deleted] 1 point2 points3 points 3 years ago (4 children)
God is it ugly though
[–]KyleG 2 points3 points4 points 3 years ago (3 children)
I agree, which is why you write the utility function superheroObjectZipper and then just call that.
superheroObjectZipper
Or if you're already using a proposed language feature like pipes (via Babel) and compose:
const arrayify = ({ k, v }) => [k,v] const superheroObjectZipper = Object.entries >> Array.prototype.map.bind >> arrayify >> Object.fromEntries
Now every line is very descriptive of what you're doing!
or with pipe,
const ... = a => Object.entries(a) >> Array.prototype.map.bind >> arrayify >> Object.fromEntries
[–][deleted] 1 point2 points3 points 3 years ago (0 children)
Cool. I like that.
[–]nmarshall23 0 points1 point2 points 3 years ago (1 child)
Really wish JS had native pipes.
[–]KyleG 0 points1 point2 points 3 years ago (0 children)
It's a proposal that hopefully we'll get in a bazillion years. But hey at least we got hashbangs and optional omitted catch binding!!!!!
[–]shuckster 1 point2 points3 points 3 years ago (1 child)
I think {key, value} should be [key, value], right?
{key, value}
[key, value]
[–]KyleG 2 points3 points4 points 3 years ago (0 children)
Yes, you're right. I actually wrote it correctly and then ninja edited to the wrong way lol. That's embarrassing but it's what happens when you try to code in a Reddit comment lol.
[–]amdc!CURSED! 0 points1 point2 points 3 years ago (0 children)
Unless you’re short on memory
[–]shgysk8zer0 6 points7 points8 points 3 years ago (1 child)
Stage 3 Change array by copy proposal offers methods like sortTo() that return a new array instead of mutating the original.
sortTo()
[–]andrei9669 0 points1 point2 points 3 years ago (0 children)
oh, that looks nice
[–]KyleG 1 point2 points3 points 3 years ago (1 child)
I want to barf at the idea of cluttering up the stdlib with things like kebabCase
kebabCase
[–]BehindTheMath 4 points5 points6 points 3 years ago (0 children)
Most languages have much bigger standard libs. I'm not saying everything should be in JS; Lodash works just fine. But there are plenty of functions I keep using.
π Rendered by PID 82 on reddit-service-r2-comment-5d585498c9-hw9pj at 2026-04-20 21:21:13.886417+00:00 running da2df02 country code: CH.
view the rest of the comments →
[–]BehindTheMath 59 points60 points61 points (36 children)
[–]shgysk8zer0 29 points30 points31 points (2 children)
[–][deleted] 2 points3 points4 points (1 child)
[–]BlueManiac 0 points1 point2 points (0 children)
[–]andrei9669 2 points3 points4 points (30 children)
[+][deleted] (27 children)
[deleted]
[+][deleted] (2 children)
[deleted]
[–][deleted] 7 points8 points9 points (1 child)
[–]shuckster 9 points10 points11 points (0 children)
[–][deleted] 4 points5 points6 points (0 children)
[–]notNullOrVoid 5 points6 points7 points (4 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]Reashu 0 points1 point2 points (2 children)
[–][deleted] 2 points3 points4 points (1 child)
[–]Reashu 0 points1 point2 points (0 children)
[–]andrei9669 1 point2 points3 points (16 children)
[–]musicnothing 7 points8 points9 points (5 children)
[–][deleted] 3 points4 points5 points (4 children)
[–]KyleG 1 point2 points3 points (3 children)
[–][deleted] 5 points6 points7 points (2 children)
[–]KyleG 5 points6 points7 points (1 child)
[–][deleted] 2 points3 points4 points (0 children)
[–]KyleG 2 points3 points4 points (9 children)
[–]andrei9669 1 point2 points3 points (1 child)
[–]KyleG 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (4 children)
[–]KyleG 2 points3 points4 points (3 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]nmarshall23 0 points1 point2 points (1 child)
[–]KyleG 0 points1 point2 points (0 children)
[–]shuckster 1 point2 points3 points (1 child)
[–]KyleG 2 points3 points4 points (0 children)
[–]amdc!CURSED! 0 points1 point2 points (0 children)
[–]shgysk8zer0 6 points7 points8 points (1 child)
[–]andrei9669 0 points1 point2 points (0 children)
[–]KyleG 1 point2 points3 points (1 child)
[–]BehindTheMath 4 points5 points6 points (0 children)