Loading topic
map, filter, and reduce work only because JavaScript treats functions as values. This chapter covers that model: what a higher-order function is, what a callback is, and who calls whom. Later chapters build on it.2 min readmap, filter, and reduce chapters rely on: these methods build a new result and leave the original array as it was.2 min readmap, filter, or reduce calls your function, which values does it pass in?2 min readmap itself: what it does, what it returns, and the mistakes people make with it mos2 min readfilter is the method you use when the question is "which of these elements do I keep?" rather than "how do I change each one?"2 min readmap and filter. map and filter both return arrays. reduce can return anything: a number, a string, an object, or another array. It does this by carrying one value, the accumulator, through the whole array a2 min readreduce calls its callback with an accumulator and the current element. Here the focus is the second argument to reduce, the initial value. That argument decides the accumulator's starting type, which element the callback sees first, and what happens when2 min readmap, filter, and reduce work on their own, including why reduce usually needs an initial value. Here you will combine them into one expression that turns raw data into a final result.2 min readreduce passes an accumulator from one call to the next, and why you should pass an initial value. In this chapter that initial value will be an empty object {} or an empty Map, so reduce builds a lookup structure instead of a number.2 min readmap, filter, and reduce call their callbacks, and that each callback receives (element, index, array). The three bugs below come from that calling convention, from the difference between a new array and new objects, and from arrays with holes in them2 min readmap, filter, and reduce work on arrays and how to chain them. You should also be comfortable with array destructuring in function parameters, such as ([key, value]) => ....2 min readmap, filter, and reduce work from earlier chapters. It also assumes you can read Promises and async/await. The examples use top-level await, so run them as ES modules: save them as .mjs files, or use a browser <script type="module">.2 min readmap, filter, and reduce work, how to chain them, and what arguments a callback receives. It covers five related methods. Each one handles a common job more directly than the big three, and sometimes with less work.2 min readfilter, map, and reduce into a pipeline, and that you can write a plain for or for...of loop. The question here is a practical one. When does a method chain cost more than it gives you, and when should you write a loop instead?2 min read