JavaScript Array map() Method


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

The JavaScript array map() method calls the specified function for every array element and returns the new array. This method doesn't change the original array.

The map() method in JavaScript creates an array by calling a specific function on each element present in the parent array. It is a non-mutating method. Generally map() method is used to iterate over an array and calling function on every element of array.

Syntax

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

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);
}

Example -