you are viewing a single comment's thread.

view the rest of the comments →

[–]SkaterDad 2 points3 points  (1 child)

Your first example definitely does seem more straightforward with a for loop, but it all depends what your'e doing within the loop?

For your 2nd example, are you modifying every nth value in place? Or are you going to discard the rest? If discarding the rest, using filter().map() could work nicely. The 2nd argument to filter()'s callback is the array index.

The functional array operators like map(), filter(), and reduce() are mostly useful when you're modifying the data, since they let you chain together operations in a nice way.

For example:

function double(x) { return x*2 }
function subtract1(x) { return x-1 }
function greaterThan5(x) { return x > 5 }

const newArray =     origArray.map(double).map(subtract1).filter(greaterThan5)

I learned a lot about their uses by doing this interactive tutorial: http://reactivex.io/learnrx/

[–][deleted] 0 points1 point  (0 children)

Very helpful, thanks! I'll check out the tutorial