What is mapping in JS | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is mapping in JS

I am having difficulties understanding map() in js

20th Apr 2020, 6:10 AM
Adnan Mukhtar Muhammad
Adnan Mukhtar Muhammad - avatar
6 Answers
+ 3
Okay, so map() method is used to map over the array and return something for each element in it. for example let arr = [1,2,3,4,5]; console.log(arr); arr = arr.map((val) => val + 1); console.log(arr);
20th Apr 2020, 6:22 AM
Raj Chhatrala
Raj Chhatrala - avatar
+ 4
Yes, map function takes a callback function as argument with takes value which is equal to element each iteration So 1 will be added to each of element like 1 becomes 2 and 2 is 3.
20th Apr 2020, 6:28 AM
Raj Chhatrala
Raj Chhatrala - avatar
+ 4
While Rick's answer is good, please let me provide more context : https://code.sololearn.com/WwyR3SGoozeD/?ref=app Firstly, map does not change the original array. It returns a new array. Secondly, the callback can be explicitly declared and with function keyword. Thirdly, the callback can take up to 3 arguments, namely item, index and the array itself.
20th Apr 2020, 7:00 AM
Gordon
Gordon - avatar
+ 1
Are you talking about Array method map()?
20th Apr 2020, 6:15 AM
Raj Chhatrala
Raj Chhatrala - avatar
0
yes sir
20th Apr 2020, 6:18 AM
Adnan Mukhtar Muhammad
Adnan Mukhtar Muhammad - avatar
0
Thanks sir, buh i am still a kindda confuse here. according to the code you wrote, 1 will be added to all the elements in the array?
20th Apr 2020, 6:26 AM
Adnan Mukhtar Muhammad
Adnan Mukhtar Muhammad - avatar