JavaScript Nuggets — Map
In this story, I will explain JavaScript methods. First of all, I began investigating map().
map() returns a new array and uses values from the original array when making a new one. It behaves as a for loop but the difference is that it uses less code and makes your code more readable.
const people = [
{
name: 'Ceren',
age: 29,
},
{
name:'Esra',
age:34,
},
{
name:'Aslı',
age:20,
}
];const ages = people.map((person)=>{
return person.age;
});console.log(ages); //[29,34,20]
In this example, map() takes each element from the array.
const people = [
{
name: 'Ceren',
age: 29,
},
{
name:'Esra',
age:34,
},
{
name:'Aslı',
age:20,
}
];const newPeople = people.map((person)=>{
return {
newName: person.name.toUpperCase().split("").reverse().join(""),
newAge: person.age+20,
}
});console.log(newPeople);
//CONSOLE[
{
newName: 'NEREC',
newAge: 49,
},
{
newName:'ARSE',
newAge:54,
},
{
newName:'ILSA',
newAge:40,
}
In another example, map() transforms it with a function that you specify and creates a new array. But here it should be noted the size of the original array doesn’t change.
I tried to explain map() method which is one of the JavaScript methods. If you want to see more details, you can review my codes on my Github profile.
If I have any mistakes, please feedback to me.
Thank You!
You can reach out to me here,
LinkedIn: https://www.linkedin.com/in/cerensolpan/
GitHub: https://github.com/cerensolpan