Map Method ::::::::::::::::::::::::::::::: Javascript

  Map Method

                                  ..........................................................................................


The map() method creates a new array with the results of calling a function for every array element. The map() method calls the provided function once for each element in an array, in order. Note: map() does not execute the function for array elements without values.


....................................................................................................................................................

....................................................................................................................................................



Definition and Usage


The map() method creates a new array with the results of calling a function for every array element.

The map() method calls the provided function once for each element in an array, in order.

Note: map() does not execute the function for array elements without values.

Note: this method does not change the original array.

....................................................................................................................................................

....................................................................................................................................................


Syntax

array.map(function(currentValue, index, arr), thisValue)

....................................................................................................................................................

....................................................................................................................................................


Parameter Values

ParameterDescription
function(currentValue, index, arr)Required. A function to be run for each element in the array.
Function arguments:
ArgumentDescription
currentValueRequired. The value of the current element
indexOptional. The array index of the current element
arrOptional. The array object the current element belongs to
thisValueOptional. A value to be passed to the function to be used as its "this" value.
If this parameter is empty, the value "undefined" will be passed as its "this" value

Another Example


var persons = [
  {firstname : "Malcom", lastname: "Reynolds"},
  {firstname : "Kaylee", lastname: "Frye"},
  {firstname : "Jayne", lastname: "Cobb"}
];


function getFullName(item) {
  var fullname = [item.firstname,item.lastname].join(" ");
  return fullname;
}

function myFunction() {
  document.getElementById("demo").innerHTML = persons.map(getFullName);
}







Comments

Popular posts from this blog

Rails 7 Features :: Comparison with Rails 6 and Rails 5