Yup! Totally makes sense. I agree that using reduce here is overkill.
However, my goal is to show two things:
How to understand how accumulator and current value change each iteration
That reduce can return either: an array, an object, or a primitive value.
I can probably think of another case where reduce is more useful than map/filter, but I'll have to change the example. I think it's easier to grasp of I use one array example throughout the whole article. It was a huge revelation when I first learned that reduce can return array or object and I hope to convey the same to readers :)
Really appreciate the feedback!
For those who read this, filter is the better tool for this situation. But the point stands that reduce can give the same result, just more work is required.
A good example would be a reduce which does both a map and a filter - if you did the same with map, then filter, you'd have to iterate over the array twice.
A cup of coffee and a good talk about tech, productivity, travel and programming. A bit of my day-to-day, Ruby on Rails, Javascript, React, frameworks and other hyped technologies
I get what you mean! I think itโs important to make it clear thatโs not the best solution since in this case reduce will be more memory consuming. Keep the good work!
Yup! Totally makes sense. I agree that using reduce here is overkill.
However, my goal is to show two things:
I can probably think of another case where reduce is more useful than map/filter, but I'll have to change the example. I think it's easier to grasp of I use one array example throughout the whole article. It was a huge revelation when I first learned that reduce can return array or object and I hope to convey the same to readers :)
Really appreciate the feedback!
For those who read this, filter is the better tool for this situation. But the point stands that reduce can give the same result, just more work is required.
A good example would be a reduce which does both a map and a filter - if you did the same with map, then filter, you'd have to iterate over the array twice.
I updated the post to include a basic map/ filter case. Thank you so much for the suggestion!
I hope it conveys the point :)
I get what you mean! I think itโs important to make it clear thatโs not the best solution since in this case reduce will be more memory consuming. Keep the good work!
I updated the post so it now includes your filter example ๐