JavaScript Array မြေပုံ()
ဥပမာများ
ဒြပ်စင်တန်ဖိုးများအားလုံး၏ နှစ်ထပ်ကိန်းရင်းဖြင့် အခင်းအသစ်တစ်ခုကို ပြန်ပေးပါ။
const numbers = [4, 9, 16, 25];
const newArr = numbers.map(Math.sqrt)
array တစ်ခုတွင် တန်ဖိုးအားလုံးကို 10 ဖြင့် မြှောက်ပါ။
const numbers = [65, 44, 12, 4];
const newArr = numbers.map(myFunction)
function myFunction(num) {
return num * 10;
}
အောက်တွင် နောက်ထပ် ဥပမာများ။
အဓိပ္ပါယ်နှင့် အသုံးပြုမှု
map()
array ဒြပ်စင်တိုင်းအတွက် function တစ်ခုခေါ်ခြင်းမှ array အသစ်တစ်ခုကို ဖန်တီးသည်။
map()
array တစ်ခုရှိ element တစ်ခုစီအတွက် function တစ်ခုကို တစ်ကြိမ်ခေါ်သည်။
map()
ဗလာဒြပ်စင်များအတွက် function ကို execute ပါဘူး။
map()
မူရင်း array ကို မပြောင်းလဲပါ။
အထားအသို
array.map(function(currentValue, index, arr), thisValue)
ကန့်သတ်ချက်များ
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. |
တန်ဖိုးကို ပြန်ပေးသည်။
ရိုက်ပါ။ | ဖော်ပြချက် |
ဇွတ်တစ်ခု | array element တစ်ခုစီအတွက် function တစ်ခု၏ရလဒ်များ။ |
Browser ပံ့ပိုးမှု
map()
ECMAScript5 (ES5) အင်္ဂါရပ်တစ်ခုဖြစ်သည်။
ES5 (JavaScript 2009) ကို ဘရောက်ဆာအားလုံးတွင် အပြည့်အဝထောက်ခံသည်-
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |
နောက်ထပ် ဥပမာများ
လူတစ်ဦးစီအတွက် အမည်အပြည့်အစုံကို ရယူပါ-
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(" ");
}