Loading topic
map, filter, and reduce one at a time, this chapter covers that shared mechanism: functions treated as values, functions that acc5 min readfor loop. It also assumes the previous chapter's idea that map, filter, and reduce take a callback. Here the focus is what these methods do to the array you call them on. They leave it alone, and a lot5 min readmap, filter, and reduce call a function you supply. Here the question is narrower: on each call, which arguments does that function receive, and what goes wro5 min readmap passes each callback three arguments: the element, the index, and the array. Earlier chapters covered those ideas. Here the focus is on map itself: what it does step by step5 min readmap. filter follows the same calling convention as map. The difference is what it does with your callback's return value: map st6 min readmap gives you one output element for each input element. filter gives you some of the input elements. reduce has no fixed output shape. It walks the array once, carries a running value from step to step, and returns whatever that value is at the end. That value can be a number, a string, an ob5 min readreduce works: it takes a callback with an accumulator and the current element, and it returns whatever the last callback call returned. Here the focus is on the optional second argument to reduce, the initial value. That argument decides the starting accumulator6 min readmap, filter, and reduce work on their own: what each passes to its callback, what each returns, and why reduce needs an initial value when the array might be empty. Here the focus is on combining them. You chain the calls into a pipeline, where each 6 min readreduce walks an array with an accumulator, and why you pass an initial value. Here the initial value is an empty object or a Map, and the result is a lookup structure instead of a number. Three patterns cover most of the work: counting, grouping, and ind5 min readmap, filter, and reduce work, that callbacks receive (element, index, array), and that these methods return new arrays instead of changing the original. Each bug below comes from code that looks correct and runs without errors but returns the wrong r5 min readmap, filter, and reduce, but objects do not. This chapter shows how to turn an object into an array, run the usual methods on it, and turn the result back into an object5 min readmap, filter, and reduce work with ordinary synchronous callbacks. It also assumes you can read Promises and async/await. Every example uses top-level await, so save it as an ES module (for example example.mjs) and run it with a recent Node.js, 5 min readmap, filter, and reduce. Some jobs fit those three poorly. You might reach for reduce to answer a yes/no question, or write filter(...)[0] to get one item. Both work, but they say less than they could and often do more work than they need to. Jav4 min readmap, filter, reduce, and chaining them, as covered in earlier chapters. It also assumes you know basic for and for...of loops and async/await. The question here is a practical one. Array methods usually read better than loops, but sometimes5 min read