All of these tutorials are super misleading as they don’t even begin to touch on how useful the reduce method really is. I usually use the example of grouping items by type.
No. It’s an array method and it will run on any array. Also, another hallmark is that it will return whatever you want. In my example, I passed an array and wanted an object in return with properties of each type that was pulled off the data. Then in those properties, arrays that have the grouped items. I gave it an array and returned an object. You can give it an array and return a number. You can give it an array and return an array. You can give it an array of numbers and return an object with whatever data you want on it. You can give it an array of Booleans and return a number. It doesn’t matter. It’s incredibly flexible because it remembers what the last item in the array was. As opposed to array.map which only iterates and returns an item for each spot. It doesn’t change the length of the array but you can mutate things as you see fit. The best scenario is to try and categorize these methods by what they can/can’t do. Then make some flash cards and then go over them multiple times. Then you can add these to your toolbelt. When learning online, tutorials give us a hammer only. And if you try to use a hammer to change the oil in your car, you’re gonna have a bad time.
5
u/[deleted] Mar 24 '20
mdn explains this shit better than any of these silly videos