r/learnjavascript • • Mar 24 '20

Array Reduce Explained in 80 Seconds

https://youtu.be/lxT4LZdtJrU
70 Upvotes

25 comments sorted by

10

u/Abiv23 Mar 24 '20

big portion missing from this 'tutorial', reduce also has a third parameter (accumulative total, current item, current index)

2

u/Mises2Peaces Mar 24 '20

Do you current index a lot in reduce?

2

u/[deleted] Mar 24 '20

i dont

2

u/Mises2Peaces Mar 24 '20

Me neither. I'm wondering if I'm missing something useful.

1

u/[deleted] Mar 24 '20

Well check out mdn reduce so you'll see when it would be useful to use the index.

i think I've needed to use it once but that was for some problem on codewars

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.

3

u/The-God-of-Thunder Mar 24 '20

Nothing beats MDN to be fair.

2

u/[deleted] Mar 25 '20

I always use MDN but this is a good resource too https://javascript.info/

2

u/kuse Mar 24 '20

I really enjoyed this! Thank you for making this!

2

u/oreo27 Mar 24 '20

My colleagues accuse me of overusing reduce (guilty!) but I love how you can do map, filter, flatten and a bunch of other things in a single reduce call. If written well, you also avoid unclear code.

3

u/[deleted] Mar 24 '20

this wasn’t really an explanation (more like a demonstration) but it was nice to watch.

1

u/rolangonzalez Mar 24 '20

Bored of the same demonstration once and once again.

5

u/IonelLupu Mar 24 '20

over and over again* :)

2

u/rolangonzalez Mar 27 '20

Thanks! Now I'm closer to be acceptable at writing English.

1

u/Macaframa Mar 24 '20

Look at my example on another comment in this thread. This is an example I give people when they’re learning about these methods.

1

u/The-God-of-Thunder Mar 24 '20

Note to self - How not to do a tutorial video.

1

u/philipjfrizzle Mar 24 '20

Reduce can do much more than sum. I like to think of reduce as a sandbox where you can basically do anything you want with an array.

You will always reduce your array down to a single item, but that item doesn’t just have to be an integer. It could be an object or another array.

Often times when I have to operate on two arrays, I like to use reduce to create an object with the info I need from one of the arrays. Then with the other array, I iterate thru it, with a .forEach for ex. I use the object I just created from the other array to lookup the values needed. This keeps the operation linear, as opposed to a quadratic operation with a nested loop.