How to use Javascript's Map() method
Hi guys! I'm back with a tutorial about the javascript Map() method. To me, in the basic functionability, it's kinda similar to the Filter() method. It's used to reduce repetition in your code by making a new array by calling a function for every array element. I'll give you an example.
Let's say here we have an array of 5 different popular Youtubers, and we want to return a new array with only the names of the youtubers. So, we do this:
We make a new array called test, and inside it we map the youtubers array and return each youtuber's name by a function called tubers. Then, we console.log the code and here is the outcome:
Now, we have a nice little array of each youtuber's names. you can also do this with other values from the main array. Let's say that we want to return the names of each youtubers and how many subscribers they have.
what I did above was the same thing I did before, but now I am returning a value of the youtuber's name and how many subs they have through a dollar syntax and a curly bracket inside a backtick. This method is called a template string, a new literal syntax introduced in es6. You can learn more about them here. anyways, here's the outcome:
pretty easy, right? If you want to see more programming tutorials like this, let me know about it in the comments. Also, subscribe to our blog so you'll get an email everytime I upload another blog post. Bye!
Comments
Post a Comment