Array Map in JavaScript. A simple method made simpler

I am a full stack developer, YouTuber and blogger!
Search for a command to run...

I am a full stack developer, YouTuber and blogger!
Hey all ๐๐ Webhooks are a powerful tool that allow applications to communicate with each other in real-time. They are often used to send notifications or updates from one system to another without requiring manual intervention. If you know the bas...

Hey everyone! It's been a while since I wrote my last article. But here I am with another topic that might be informational to you ๐. This time I am writing about why you should avoid the onclick attribute in your HTML and JavaScript code. This ar...

โ What and Why Memoriez โ This is a very important question because it makes things clear as to why this app is needed. I built Memoriez because I honestly found it hard to write and maintain journal/diary entries daily. It's not easy to maintain a j...

Explained with a simple project

๐ Hey developers! This post is about how you can sort import statements in your JavaScript/TypeScript/React/Node etc projects easily with Prettier in VS Code when you format the code. https://www.youtube.com/watch?v=QQWgN0_gUxI What will you achieve...

The
map()method creates a new array populated with the results of calling a provided function on every element in the calling array.
Seems complicated right? But it isn't. Let's see how.
If you know the forEach() array method in JavaScript then it would be easy for you to understand the map() method.
map() method is an array method which LOOPS OVER AN ARRAY and CREATES A NEW ONE as per the changes defined.The process is as follows:
Let's understand this by an example
Say you have an array of words which are in present tense:
var presentWords = ["work", "play", "click", "end", "start"];
and you want to create a new array that will contain the same words but in past tense. We will add an ed after each word for that. So we will define a new variable like this.
var pastWords = presentWords.map(function(word) {
return word + "ed";
});
Here, we have a callback function that has access to every item in the array and we concatenate ed to each word and return it.
If we log pastWords to the console, we will get this:
["worked", "played", "clicked", "ended", "started"]
Now we did this in an old fashioned way of JavaScript. Let's make this look more clear with the ES6 arrow function, template strings, and constants
const presentWords = ["work", "play", "click", "end", "start"];
The arrow function can be like this:
const pastWords = presentWords.map(word => {
return `${word}ed`;
});
But we can get rid of the return and the extra line and make it like this:
const pastWords = presentWords.map(word => `${word}ed`);
So we transformed the code from this:
var presentWords = ["work", "play", "click", "end", "start"];
var pastWords = presentWords.map(function(word) {
return word + "ed";
});
to this:
const presentWords = ["work", "play", "click", "end", "start"];
const pastWords = presentWords.map(word => `${word}ed`);
I also have a video on my channel about the array.map().
I hope you enjoyed and learned from this blog post. If you have any suggestions or feedback. Please let me know in the comments. Happy Coding