r/learnjavascript • • Mar 24 '20

Array Reduce Explained in 80 Seconds

https://youtu.be/lxT4LZdtJrU
70 Upvotes

25 comments sorted by

View all comments

5

u/[deleted] Mar 24 '20

mdn explains this shit better than any of these silly videos

7

u/Macaframa Mar 24 '20 edited Mar 27 '20

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.

const items = [
  {
    name: “toilet paper”,
    type: “bathroom”,
    amount: 3,
    price: 13
  },
  {
    name: “tooth brush”,
    type: “bathroom”,
    amount: 1,
    price: 6
  },
  {
    name: “67 piece cutlery set”,
    type: “kitchen”,
    amount: 1,
    price: 117
  },
  {
    name: “8 pack black ballpoint pens”,
    type: “office”,
    amount: 1,
    price: 4
  },
  {
    name: “post it notes”,
    type: “office”,
    amount: 2,
    price: 3
  }
];

const orderGroupedByType = items.reduce((accumulator, {name, type, amount, price}) => {
  if(!accumulator[type]) {
    accumulator[type] = [];
  }
  accumulator[type].push({name, amount, price });
  return accumulator;
}, {});

This is a super useful example of reduce.

1

u/[deleted] Mar 24 '20

I'll have a look at this. Thanks

1

u/Macaframa Mar 24 '20

Yep. Just step through the logic and console log if you have to along the way to understand what is going on. Then try to manipulate those data in other creative ways. Remember, as front end engineers, our job is to display data. Taking data from a backend and shaping it how we need it to display is apart of the job.

1

u/Jmarch0909 Mar 24 '20

So is reduce only used with arrays that contain objects?

2

u/Macaframa Mar 24 '20

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.

2

u/Jmarch0909 Mar 25 '20

Appreciate that detailed response dude 👌🏻👌🏻👌🏻

1

u/Macaframa Mar 25 '20

Yw. Now get out there and be great.