Hello,
Shouting out to anyone who is well versed with JS and can help me with this query.
Can anyone tell me why the Reducer method in Javascript is called 'Reducer', when it exactly does the opposite...doing additions in array.
I searched online for the answer but did not find any and this particular question is driving me crazy.
Thanks for the advance help.
Top comments (2)
The reducer takes a collection (array) of inputs and reduces it to one output.
Actually, you don't have to return a single output.
You can totally re-define the other popular array functions like
map
,filter
etc... Only withreduce
.As for the question, my guess (because I could not find enough answers except for this stackoverflow question) is that it is called like that (or
fold
, oraggregate
) because as I said, you can actually usereduce
to define all sort of array methods with it.And I guess the term is representing pretty well all sorts of operations that are possible to do with it. But whether you call it
reduce
orfold
oraggregate
, it won't match 100% what you are trying to do at the moment, because some times you are filtering, some times you are mapping, etc...It is a tradeoff term I guess and most of the time, you won't be using
reduce
to map, since there is already a definition of themap
method. Same thing forfilter
and the other methods.