16.8: Array Functions: filter() – Topics of JavaScript/ES6
- January 9, 2024
- Posted by: MainInstructor
- Category: Go JavaScript
Video Title: 16.8: Array Functions: filter() – Topics of JavaScript/ES6
All right I’m back for another higher-order function with JavaScript arrays video and in this video I will talk about filter filters actually to be honest it might be my favorite one of these it’s just it’s lovely so let’s look at what let’s so filter what does the word filter means it’s it’s called
Filter for a reason just like you might filter your water and filter out all the stuff you don’t want if you have an array that has some grime and stuff in it you could filter it out so how do you do that it’s a higher order function so
What that means is it expects as an argument some function and what that function is it’s a function that receives as an argument itself an element of the array and based on that element it should return true or false and I always forget this I think it
Returns true if you want to keep it and false if you don’t want to keep it so even though it’s a filter you’re thinking true I would have filter it out I think it’s true I want to keep it we’ll have to try and look up and
Documentation ok and we’re gonna do the same thing so this is where are we last left off with this reduce stuff she’s still here after that thank you and so what I’m going to do is I’m going to keep this same little silly array and I’m gonna say I’m gonna write a function
And I’m gonna say is you know interesting let’s hmm let’s call this function is even so what this function is going to do is it’s going to take a number and it will return true if it’s an even number or false if it’s an odd
Number so it’s going to if num and how do I do this I can use the modulus operator the modulus operator see the coding train video from go on Levin about modulus is an operator that gives you the remainder of division so if you divide something by two and get a
Remainder of zero it’s an even number so if num modulus two equals zero then I want to return true otherwise I want to return false now I always do this because this is the kind of person I I will write it in seven lines of code to make it say exactly what
Doing even if it can be done in one line of code but this is this is a moment where I think it’s worth today these videos are a little bit about reducing the amount of typing it’s worth saying that I don’t need to say if this is true
Return true if it’s true just return the fact that it’s true so this evaluates a true or false I’m gonna just return that so I can do this now I should be able to say what’s I should be able to say Vale’s dot reduce is even so the
Question is is my array going to have only left in it the even numbers or the odd numbers we could look it up in the documentation I could speculate but we could also just try it now I’m pretty oh this didn’t say reduce this should say filter of course I should be filtering
Based on this function hello and I’m pretty sure let’s just double check I’m pretty sure that this is the case where it creates a new array and returns back like I like we saw with the map function so if I save this here and I hit refresh
We’re say okay so that’s the original array so this is not working yet I want to say Val’s equals the new erase I’m going to overwrite that variable with the new array that’s been filtered and I’m gonna hit refresh and there we only have the even ones left so we have the
Even ones left because this returned true so if it returns true keep the value if it returns false don’t keep the value and by the way what I’m gonna do with this later in like a particle system example is I have an array of all these particle objects I could just say
Filter out all the ones that are off the screen or filter all the ones out that alpha has faded out to zero so that’s why this filter can become really really useful so let’s now shorten this using es6 syntax one thing I might want to do is
Just use this as X right change this to X now I’m gonna get rid of function I’m gonna get rid of the name I’m gonna put the arrow syntax in I’m gonna forget about this and I’m gonna get rid of the return because it’s assumed so basically
This is what I can do right this is basically I can now write that I do that really quickly because I’m kind of assuming you’ve watched my previous videos where I stay through that more slowly but basically I can say hey filter every value X based
On the weather X modulus two equals zero so this should give me exactly the same result again this is the thing this is why I hesitate this is so unlike me I don’t I I would write this in like ten but there’s there’s a wonder and beauty to
This because even though I feel like this is cryptic and a little harder to follow there is a moment I think after you get used to this arrow syntax and write a lot of these functions and I’m the comments are going on that I’m going
To look at in a second that you start to get used to this and this this becomes a bit more intuitive and it actually can make the code more readable look there we go it worked now I can say let’s just filter it out for the odd ones equals
One now I have the odd ones I could say not this and that would be that’s like not the odd ones there’s also the even ones and we could see there we go so wonderful I’m gonna take a look at the chat and see that I got some extra notes
In there yep okay so um alka in the chat points out that you can filter by truthy and falsy values not just explicitly true or false what does that mean in other words I could filter out let’s say I had an array of objects and a few of them were undefined
You know we’re empty I could filter out any spots where something’s undefined so many things in JavaScript can be sort of like thought of as false or true based on its its value like if an object exists it’s true if it’s undefined its false so I could say so this would say like
Hey I have an array with stuff and over on this actually oh okay so I could actually just do this right that’s what that’s what alka is telling me so I could check like Oh filter out just keep everything that’s not undefined right so this if I did this it
Would give me five four two one because I would filter out the undefined but undefined is like a falsie value so I could just filter out the items that like kind of turn into false things and actually this is going to give me yeah right so
Worked as well and I could say not X then I have autorai with just undefined okay so I think we’ve seen I think this has been good I think we’ve seen filtered filter filter filter the function filter do you have any questions we’re not actually in the same
Room at the same time because I’m recording a video there is a live chat going on let me check if there’s any questions two wonderful tips that just came in from the chat that I would love to show this is great having a live chat wide for these videos so let’s say I
Wanted to do that eat that even thing again where I’m keeping only the even values I had it like this X modulus 2 equals 0 that’s the even ones well that’s silly X modulus 2 evaluates always to 0 or 1 1 being true 0 being
False in a sort of truth a falsie way so I could have actually done this and that’s gonna keep the odd ones and if I were to say not I think I’m gonna need parentheses yeah I’m gonna need parentheses around this and now I’m gonna get the even ones so
That’s lovely then there’s a lot of times when you’re working with strings that’s that I’ve had this problem actually a ton so let’s say I have a this is gonna be useful let’s say I have a string it was a dark and stormy night and what I’m doing is I’m saying let
Words equal s dot split and I’m gonna use a regular well I’m just gonna say split by space and then I’m gonna say console dot log words so you can see ah it was a dark and stormy night now things could happen like what if I had
Two spaces here by accident and I hit refresh look at that because I had two spaces I got an empty string by accident now the truth of the matter is if you watch my videos about regular expressions I can be more thoughtful about the split what I pass into split
And use a regular expression for a particular pattern to match but even so like live item you might not know I can a break expression is a way of creating a pattern and so what I could do is say anything that’s not a word character and
What I wanted to see here is whenever I do this I always get things extra empty strings this happened even here so this is something that I do in one of my previous videos this is basically a secret code for saying split by anything that’s not a through Z or 0 through 9
And that has to do with meta characters and regular expressions but actually I’ve always having this problem where I get these extra empty strings so I could always just have a filter now filter by s s dot length by s dot length so look at this this should say cuz it’s true or
False see if this string has a not s dot length right if the string is the length of 0 I should filter it out so now let’s do this we can see oh no no that didn’t work not maybe I shouldn’t use s here let’s use X filter X not X dot length
Oh no X dot length I want X dot length ah the opposite that was right so I can use s here I just was using s in too many places because it was the string so word word dot length right so I want to use the length to evaluate the word
Whether it qualifies or not I could of course say like greater than 3 right if I want to just have only the 3 letter words this will get rid of it and a now you can see I only have darkstorm it got rid of was 2 because it’s not greater
Than or equals 2 so you can see how this filter you can chain these kind of again I’ve got these really long lines of code which is very unlike me but you can see I can chain this stuff so I take this string I split it up so I have all the
Words but you know what I actually only want the words that are three or more characters and instead of having to write a loop and an if statement and you splice how I always do it this is a nice really one line of code kind of way of
Using colter oh I’m happy about that okay so thanks for watching this video I hope this was helpful to you about filter and I will come back in one more video I might as well I believe I’ve covered sorts somewhere but maybe I’ll make a standalone video that’s just
About the sort function and that’ll be the last one that I’ll do in terms of these higher-order array functions thank you
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
ur a goat
Finally I got best coding teacher 😊
Thank you so much for this educational video, I learned so much.
I'm really thankful to you for making this super easy. Your energy never makes any lesson boring. I wish there exists professors like you in my univ. Thanks for rescue captain.
Thank you so much. You explaining everything so simple. Greetings from Germany!
I was having a big time understanding how to use filter, and your video made it click almost instantly. Thank you so much. Subbed, because this quality content is what i need in my life <3
Such a great video.
Awesome Teaching dude
Omg first time ive seen a teacher that good u get a sub rn
Great style! Always a pleasure.
9:06 using filter for that is a total overkill. Just continue using regex to turn it into an array without an empty value like this: s.split(/s+/);
U got my subscription
This man is the best.
you can think like upper filter come with us and drainage goes
.filter() goes over each element in the array and copies it into a new array if the predicate is true on the current vaue.
thank you!
Nice work 👍
Thank you WAPTECS for helping me bypass the 2 step verification ôn my ps4 account I'm grateful..
My account got disabled for nó reason, then a friend introduce me to WAPTECS who got my accóunt recovered successfully,🤗
I couldn't get this to work so naturally I followed the comments and WAPTECS unlocked my phone. Thanks fór the recommendations, guys.
Wow I really love WAPTECS he was the best tô buy dump and pin I got my own without any complaining 💯✅