JavaScript Dizi haritası()
Örnekler
Tüm eleman değerlerinin karekökünü içeren yeni bir dizi döndürün:
const numbers = [4, 9, 16, 25];
const newArr = numbers.map(Math.sqrt)
Bir dizideki tüm değerleri 10 ile çarpın:
const numbers = [65, 44, 12, 4];
const newArr = numbers.map(myFunction)
function myFunction(num) {
return num * 10;
}
Aşağıda daha fazla örnek.
Tanım ve Kullanım
map()
her dizi öğesi için bir işlev çağırarak yeni bir dizi oluşturur.
map()
dizideki her eleman için bir fonksiyon çağırır.
map()
boş öğeler için işlevi yürütmez.
map()
orijinal diziyi değiştirmez.
Sözdizimi
array.map(function(currentValue, index, arr), thisValue)
parametreler
Parameter | Description |
function() | Required. A function to be run for each array element. |
currentValue | Required. The value of the current element. |
index | Optional. The index of the current element. |
arr | Optional. The array of the current element. |
thisValue | Optional. Default value undefined .A value passed to the function to be used as its this value. |
Geri dönüş değeri
Tip | Açıklama |
bir dizi | Her dizi öğesi için bir işlevin sonuçları. |
Tarayıcı Desteği
map()
bir ECMAScript5 (ES5) özelliğidir.
ES5 (JavaScript 2009) tüm tarayıcılarda tam olarak desteklenir:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |
Daha fazla örnek
Her kişi için tam adı alın:
const persons = [
{firstname : "Malcom", lastname: "Reynolds"},
{firstname : "Kaylee", lastname: "Frye"},
{firstname : "Jayne", lastname: "Cobb"}
];
persons.map(getFullName);
function getFullName(item) {
return [item.firstname,item.lastname].join(" ");
}