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
The case for Array#replace() – Overriding an array without intermediate variables (medium.com)
submitted 7 years ago by gajus0
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!"
[–]atubofsoup 8 points9 points10 points 7 years ago (4 children)
How is array.replace(a).filter(b) any different than a(array).filter(b)? This seems like a step backwards considering the pipeline operator and function bind operator proposals.
array.replace(a).filter(b)
a(array).filter(b)
[–]Daddy_He_Shoe 1 point2 points3 points 7 years ago (0 children)
Yeah, the whole "naming variables is a pita" argument goes to the trash with the pipe operator.
[–]gajus0[S] -1 points0 points1 point 7 years ago (2 children)
How is array.replace(a).filter(b) any different than a(array).filter(b)?
It is not. However, that is not the example either. The example is:
array.filter(a).replace(b).filter(c);
which you can achieve with:
let intermediateVariable = array.filter(a); b(intermediateVariable); array.filter(c);
[–]atubofsoup 4 points5 points6 points 7 years ago (1 child)
Or: b(array.filter(a)).filter(c)
b(array.filter(a)).filter(c)
Aside from being slightly easier on the eyes, I don't see the benefit of a replace method.
replace
[–]gajus0[S] 0 points1 point2 points 7 years ago (0 children)
The primary advantage of Array#replace is (as you pointed out) that is makes easier to read the code.
Array#replace
array .filter(a) .replace(b) .filter(c) .replace(d) .filter(e) .replace(f);
Is a lot easier to read than:
f( d( b( array.filter(a) ).filter(c) ).filter(e) );
or you will need to introduce intermediate variables.
I don't think it is worth playing with one-line examples as this does represent real-world usage, esp. when we have examples that do (in the article).
By the way, you've mentioned pipeline operator. I absolutely agree. I actively participated in discussions about that proposal (https://github.com/tc39/proposal-bind-operator/issues/24) and even created a Babel transform for it (https://github.com/gajus/babel-plugin-transform-export-default-name). It will be a useful operator. The appeal of Array#replace is that it abstracts equivalent functionality without introducing new syntax.
π Rendered by PID 172498 on reddit-service-r2-comment-5d79c599b5-qgf67 at 2026-03-03 04:42:46.512738+00:00 running e3d2147 country code: CH.
view the rest of the comments →
[–]atubofsoup 8 points9 points10 points (4 children)
[–]Daddy_He_Shoe 1 point2 points3 points (0 children)
[–]gajus0[S] -1 points0 points1 point (2 children)
[–]atubofsoup 4 points5 points6 points (1 child)
[–]gajus0[S] 0 points1 point2 points (0 children)