What is map ?
It returns a new array without changing the original array.
Example, Lets double the numbers using map
const numbers = [10,20,30,40,50];
const newNumbers = numbers.map((number) => {
return number * 2;
});
Task -
- Find a new array which gives squares of this array [1,3,5,6,8,9,11,13,15,17,18]
- Lets make an array of 6 people. Then use a map to create a new array where we are saying to each person - Congratulations ! {name}, You are selected.
- convert prices from dollars to rupees [23,45,250,7.5,80,100,2500,75,88,990] - 137
- These are the bike prices in Nepal. First convert it into indian rupees. Just divide the money by 1.6 [259900, 334900, 299900, 359900, 349900, 379900, 564900, 635000, 534900, 624900];
- [0, 10, 20, 30, 35, 40] - Convert these temperatures from Celsius to Fahrenheit. F = (C * 9/5) + 32
- Lets resee array of those bike prices, Find the final price after 15% discount on each.
- Create an array of any 8 phone numbers in form “9841234567”. Then use the map to add the country code and display the number in the form +977 9841234567.
- Convert birth year into ages - [1995, 1997, 1998, 2001, 2005, 2008]
- Convert student marks into remarks - Pass or fail. Create an array of students marks with at least 8 students.
- Convert the usernames into emails - ["Ram","Shyam","Krishna","Raghav","Mohan","Keshav"] by adding @gmail.com at the end.
There are actually 3 parameters inside the callback function inside map. They are items, index and array. Lets see the simple use of index.
const students = ["Ram","Shyam","Krishna","Raghav","Mohan","Keshav","Narayan","Vishnu","Mahesh"];
const newFriends = students.map((student,index) => {
return `${index + 1}. ${student}`;
});
console.log(newFriends);
Task - Make a list of 20 students in school. And then display them roll number wise using map.