r/learnjavascript • • 12d ago

How did you get comfortable with JavaScript array methods ?

Array methods like map (), filter(), and reduce() seems useful, but sometimes I find it difficult to decide which one to use when solving a problem.

I'm trying to practice them with small example instead of just memorizing what each method does.

What helped you understand these methods better? Do you prefer solving exercises or using them in small projects?

3 Upvotes

9 comments sorted by

4

u/chikamakaleyley helpful 12d ago edited 12d ago

mmm the ones you've listed have pretty distinct use cases, so I wouldn't discount the value of memorizing what each method does

I'd argue not knowing what each does, is why you can't decide what to use given a problem

2

u/tambooby 12d ago

pretty much, the decision paralysis comes from not knowing them well enough imo

3

u/ChaseShiny 12d ago

Yeah, I've got to agree with u/chikamakaleyley: these three feel pretty different from each other normally.

Can we see your thought process? When do you think you should use filter? When would you rather reduce your array? When would you map each value to a new value?

2

u/Gold-Watercress-6101 12d ago

By doing a lot of practice and using them in my own projects. Over time I built an intuition when to use which one.

2

u/senocular 11d ago

There are visualizations out there like this one that can help demonstrate how these methods work making it more clear when you might want to use them.

In digging up the one I linked above, I noticed not many of those I've found include reduce(), probably because its so open ended letting you determine what the resulting value is. One I thought did ok is this one with emojis though I find the other examples harder to read (maybe because I'm old).

reduce() is unique because it lets you "reduce" an array into a single value, but that single value can also itself be an array. This means you could create a reduce call that does the exact same thing as a map() or a filter(). This doesn't mean you should use reduce for everything just because it can do everything. You should use it only when the other array methods can't give you what you need. Not only is it more code to write to get reduce to do those things, but code is also easier to read when you use the write tool for the job. If you see map() in code, you know its converting all values in an array to some other array of values of the same size. When you see filter() its keeping the values the same, but (potentially) giving you an array with fewer values in it back. For reduce() you just know something is coming out, but you have to spend a little extra brain power to figure out what. It could be an array with filtered or mapped values, or it could be a hamburger.

2

u/LetUsSpeakFreely 10d ago

They're the same in most every other language, so you just have to memorize it. The only one that really annoying is reduce(), it should be named something like group() or aggregate ()

1

u/Aggressive_Ad_5454 11d ago

An array typically contains a bunch of similar data items. So start by thinking “for each of these items, I want to … “

One you know what you want to do, you can choose .map(), .forEach(), or whatever.

It takes some practice to think this way. But learning it pays off big time in all languages.

1

u/TheRNGuy 5d ago

If you forget, just google it again (or ask ai)

Have a file with bunch of examples for all 3, or have bookmarks to MDN.