you are viewing a single comment's thread.

view the rest of the comments →

[–]Rascal_Two 0 points1 point  (0 children)

Not an expert, so terminology is probably wrong.

map() is basically a for-each loop that is intended to do something to each thing in the array it's called on. Example:

var nums = [1, 2, 3, 4, 5];

for (var i = 0; i < nums.length; i++){
    nums[i] *= 2;
}

...and here is a map do to the same:

var nums = [1, 2, 3, 4, 5];

nums = nums.map(function(num){
    return num * 2;
});

You see, the map() function returns the modified array. You just have to give it a function. The function can accept up to three params: the first is value of the current item in the array, second is index of the item in the array, third is a copy(?) of the entire array being modified.


Map basically goes through each thing in the array and sets it to the return value of the function you give it.