JavaScript Lessons

JavaScript Array Map: Transform Your Arrays with Ease

See how JavaScript's array map method turns one array into another by applying a function to every element, with short examples you can copy.

Serhat ALPASLAN2 min read

javascript array map

You can think of JavaScript array map as a way to transform the values inside an array. In other words, it lets you produce another array by using an existing array (array). Here’s what you can do with array map:

  • The map() method creates a new array with the results of calling a function for each array element.
  • The map() method calls the provided function once for each element in an array, in order.
  • map() does not execute the function for empty elements.
  • map() does not change the original array.

 

javascript array map örnek yazı dizisi aşağıdaki gibidir.
array.map(function(value, index, array), thisValue)

value : Stores the value of the element in the array and is required.
index : Stores the index value of the element in the array; it is not required.
array : Holds the entire array and is not required.
thisValue : The value to use as this when the function is called.

Let’s continue with an example

<p id="sonuc"></p>

<script>
const kisiler = [
{ad : "Ali ", soyad: "alininSoyadi"},
{ad : "veli", soyad: "velininSoyadi"},
{ad : "ayşe", soyad: "ayşeninSoyadi"}
];

document.getElementById("sonuc").innerHTML = kisiler.map(tamAdSoyad);

function tamAdSoyad(item) {
return [item.ad,item.soyad].join(" ");
}
</script>

Let’s continue with an example involving numbers.

<script> 
var sayiDizisi = [64,36,100,25,256];
var kareKok = [];
 
sayiDizisi.map(function (value, index) {
    kareKok[index] = Math.sqrt(value);    
 
});
console.log("Sayı Dizisi Eski : " + sayilar);
console.log("Sayı Dizisi Yeni : " + karekok);
</script>

array map function works in all browsers above Internet Explorer 8.

JavaScript ARRAY, POP, PUSH, LENGHT

JavaScript if else usage

What Is a JavaScript For Loop and How Is It Used?

What Are JavaScript onClick and onChange Events?

JavaScript break and continue usage

You can also click here to review it on the official site.

Share it if it helped

Write a comment