Array.prototype.map
The map() method creates a new array with the results of calling a provided function on every element in this array.
The map() method creates a new array with the results of calling a provided function on every element in this array.
var arr = [1,2,3,4,5,6];
The map() method creates a new array with the results of calling a provided function on every element in this array.
var newArry;
The map() method creates a new array with the results of calling a provided function on every element in this array.
var newArry = arr.map(callback);
The map() method creates a new array with the results of calling a provided function on every element in this array.
newArray = arr.map(function(element){
return element + 1;
});The map() method creates a new array with the results of calling a provided function on every element in this array.
var newArray=arr.map(function(element){
return element + 1;
});//newArray is [ 2, 3, 4, 5, 6, 7 ];var arr = [1,2,3,4,5,6];
produces new value for every element in the array
var n = [1,2,3,4,5,6];
var m = n.map(function(){
});
it takes three parameters
produces new value for every element in the array
var n = [1,2,3,4,5,6];
var m = n.map(function(element){
});
it takes three parameters
produces new value for every element in the array
var n = [1,2,3,4,5,6];
var m = n.map(function(element, index){
});
it takes three parameters
produces new value for every element in the array
var n = [1,2,3,4,5,6];
var m = n.map(function(element, index,arry){
});
it takes three parameters
produces new value for every element in the array
var n = [1,2,3,4,5,6];
var m = n.map(function(element, index,arry){
return element % 2 === 0;
});
it takes three parameters
it has a return statement, which is the new value per element
var arr = [1,2, 3];
var newArr = arr.map(function(element){
return element * 2;
});
console.log(newArr);What is the result?
var nums = [1,2,3,4,5,6];
var nums2 = nums.map(function(element, index, arr){
if(index % 2 === 0){
return element * 2;
}
else{
return element *5;
}
});
console.log(nums2);What's in nums2?
var people = [
{firstName:"Jon", lastName: "Snow"},
{firstName:"Tyrion", lastName: "Lanniser"},
{firstName:"Stannis", lastName: "Baratheon"}
];
var GoTCharacters = people.map(function(element){
return element.firstName + " " + element.lastName;
});What is the content of GoTCharacters?
var arr2 = [1,2, 3];
function mulBy10(x){
return x * 10;
}
var newArr = arr2.map(mulBy10);
console.log(newArr);What's in newArr?
Given the following array
arr = [2,3,5,6,8,9,19,12,4]
using map(), create a new array where every value is squared
result should be
[4,9,25 .... ]