Skip to main content
View on YouTube to comment and like!

Syntax Video

Goodbye Array .reduce

Chapter 1 00:00

Transcript

array. reduce is probably one of my most loved and hated parts of JavaScript but anytime there's something in the language that allows me not to write a reduce I'm a happy guy so I have this array of orders here and I want to be able to group them by which country they're coming from normally what I do is use a reduce that looks a little bit something like this where you start off with an empty object you Loop over them you check if the country code key has been set if it's not you'll set it to an empty array then we go ahead and push that order into the array and then return the accumulator from that that works if we go ahead and run that you'll see now we have an object with all of the keys of the countries and then the associated orders are with them that works okay but I find them a little bit hard to debug now we have object.

groupby you do two things you pass it your iterable which is your array and then you pass a call back which defines what your key is going to be so I'm simply just writing a function here that Returns the orders country code and that will say I want to use this item's country code as the key and throw it into a group if that already exists if not create a group there we go we've got all of our countries as keys on this object and then an array of all of the associated orders pretty straightforward this is really handy when you want to sort of filter for multiple items so often I'll want to find um filter for shirts and stickers that have been sold so what you can do is you run that same code but then just immediately destructure the pieces that you care about and then you the rest of them will be garbage collected now we also have the ability to use map. groupi maps in JavaScript are really handy when you want to use something other than like a string or a number or a symbol as the

key to that object so here I have a list of utterances which is like words that have been said in in captioning and and each one of them has a start and an end time associated with it then I also have a array of topics for the specific video and it tells tells me when each of the topics start if I want to be able to group The utterances by which topic they were talked about I can now say map.

groupby you Loop over each of the utterances and then for each of the utterances we find the topic that is is associated with it so I grab the start of the topic I grab the end of the utterance and then I find the topic where those things fit in between and now I'm able to log out a map where the key of the map is not just like like a string like topic one but the key is the actual entire topic object and then the value is is all the utterances that are associated with it that's really nice because then you can simply do something like um map. getet topic and you can pass it the entire topic object and you don't have to maintain unique identifiers for each of those specific specific topics it is not based on everybody ask me this every time I show it it is not based on the contents of the object or whatever you pass to the

key it's based on the reference to the actual topic object let's talk about browser support it's in all of the browsers all of the run times right now however if you do need to support older browsers like for example Safari 17.4 is relatively new um so what you'll want to do is use a polyfill if you use core.js or something like that to recreate the functionality it will just do it for you I've used one called object. Group by here um the way that it works is you simply just require it or import it and then run the shim method and what that will do is it will overwrite the global prototype of object so if it does not exist and it'll just recreate the functionality same thing goes for the map. groupby as well

Quote it with credit and a link. Quoting Syntax