Javascript Coding Challenge for Beginners Q9
Transcript
English (Auto-generated)
Hello everyone welcome back to my javascript coding challenge for beginners today, we're on question nine. We're gonna be using question eight for each method to solve this problem. So if you don't know what for each method is feel free to go back to the last video to get a refresher. But if not we can get started. So for today's example, we're given an array of objects called art. We want to return the names inside this array without the duplicates. So for example, we want to return john lisa, bart and ella because we don't want the double john duplicates. So pasta video and come up with their own solution. And when you're ready we can work on the solution together. Now when you're ready we can work on the solution because we want to return a list of names, we need a holder to hold this list of names. So let's call it holder are I'm not sure why it's flagging me already expecting a comma not sure why. But um oh there was a that was a big mistake, I don't know how I did not see that. So, so I had a I had a square bracket instead of a curly bracket. So moving on to the challenge, we're gonna have a for each method so are for each um as I mentioned before you can write it in the function wave, but I prefer the arrow function because it's just much shorter and for each method takes in three parameters. But for this one we're only gonna use the first parameter and we're gonna call it element. So what this does is a quick refresher for you guys to remember just that iterate through each array like a loop. So for each is basically a looping method or function. So apart, holder includes element dot me, remember that? We are looping through an object. So in order to access the value named john we have to access through the dot notation so element dot me. So if it includes element dot name, we want to return the name on our case because our object is empty. We don't want to include it because if the name is not in our holder which we're gonna use to return, then we want to push the value into the holder. So if I hold her does not have this name, you want to push it inside. So put it on to you holder. Dot push element name. So again we want to return the holder. That's why if there's a duplicate, we don't want to push it. So we only want to push the values that appear once. So let's try to run this in the terminal. Okay, lets save it first and you will see that it returns exactly john lisa bart and ella. So if it doesn't make sense, I'm going to go through it again. If holder does not include the name because our is empty we wanna push inside. But when it goes through john again because it does include john it just won't execute this function and will go through the next iteration. In the end, we're going to have a holder with a list of names. So thank you all for watching this video. When you're ready, we can move onto the next challenge. And for the next challenge is, we're gonna be applying these functions over and over again so you can build your skills on top of it.