While attempting to explain JavaScript's reduce method on arrays, conceptually, I came up with the following - hopefully it's helpful; happy to tweak it if anyone has suggestions.
forEach:
- callback answers: here’s an item. do something nutty with it, i don't care what.
- callback gets these arguments:
item,index,list - final return value: nothing - in other words,
undefined
map:
- callback answers: here’s an item. what should i replace it with?
- callback gets these arguments:
item,index,list - final return value: list of new items
filter:
- callback is a predicate - it should return a truthy or falsy value
- callback answers: should i keep this item?
- callback gets these arguments:
item,index,list - final return value: list of kept items
reduce:
- callback answers: here’s the result from the previous iteration. what should i pass to the next iteration?
- callback gets these arguments:
result,item,index,list - final return value: result of last iteration
reduceRight: (same as reduce, but in reversed order: last-to-first)
some:
- callback is a predicate - it should return a truthy or falsy value
- callback answers: does this item meet your criteria?
- callback gets these arguments:
item,index,list - final return value:
trueafter the first item that meets your criteria, elsefalse
every:
- callback is a predicate - it should return a truthy or falsy value
- callback answers: does this item meet your criteria?
- callback gets these arguments:
item,index,list - final return value:
falseafter the first item that failed to meet your criteria, elsetrue
find:
- callback is a predicate - it should return a truthy or falsy value
- callback answers: is this item what you’re looking for?
- callback gets these arguments:
item,index,list - final return value: the item you’re looking for, or undefined
findIndex:
- callback is a predicate - it should return a truthy or falsy value
- callback answers: is this item what you’re looking for?
- callback gets these arguments:
item,index,list - final return value: the index of the item you’re looking for, or
-1