ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
ဆေးဆိုင်က ဆေးဗူးထားတဲ့ စင်တစ်ခုကို စဉ်းစားကြည့်ပါ။ အကန့်တွေကို 0, 1, 2 ... လို့ နံပါတ်တပ်ထားပြီး အကန့်တိုင်းမှာ ပစ္စည်း တစ်ခုစီ ရှိပါတယ်။ "အကန့် 2 က ပစ္စည်း ယူပေးပါ" လို့ ပြောရုံနဲ့ ချက်ချင်း ရှာတွေ့ပြီး စင်ရဲ့ အဆုံးမှာ ပစ္စည်းအသစ် ထပ်တင်လို့လည်း ရပါတယ်။
JavaScript array က အဲဒီ စင်ပါပဲ။ ["Banana", "Orange", "Apple"] လို item တွေကို variable တစ်ခုတည်းထဲမှာ အစဉ်လိုက် သိမ်းပြီး fruits[0] လို index နဲ့ ဖတ်ပါတယ်။ push, map, filter လို method တွေက item ထည့်တာ၊ ပြောင်းတာ၊ ရွေးထုတ်တာကို loop ကိုယ်တိုင် မရေးဘဲ လုပ်ပေးပါတယ်။
Problem → Why → Solution
ပြဿနာ: sort() က ဈေးနှုန်းတွေကို မှားစီပြီး မူရင်းကိုပါ ပြောင်းတယ်
ဈေးအနည်းဆုံးကနေ စပြတဲ့ "Cheapest first" list တစ်ခု လိုလို့ sort() ကို ခေါ်လိုက်ပါတယ်။ ရလဒ်မှာ 100 က 9 ရဲ့ ရှေ့ ရောက်နေပါတယ်။ ပိုထူးဆန်းတာက မူရင်း prices array (ထည့်ခဲ့တဲ့ အစဉ်အတိုင်း ပြရမယ့် စာရင်း) ကပါ ပြောင်းသွားပြီး page ရဲ့ တခြား အပိုင်းတွေမှာ အစဉ် ပျက်သွားပါတယ်။
const prices = [10, 9, 100, 1];
const cheapestFirst = prices.sort();
console.log(cheapestFirst); // [ 1, 10, 100, 9 ] wrong order
console.log(prices); // [ 1, 10, 100, 9 ] the original changed tooဘာကြောင့် ဒီလို ဖြစ်တာလဲ
Comparator မပေးရင် sort() က item တွေကို string ပြောင်းပြီး စာလုံးအစဉ် (dictionary order) နဲ့ စီပါတယ် — "100" က "9" ရဲ့ ရှေ့ရောက်တာ "1" က "9" ထက် ငယ်လို့ပါ။ ဒါ့အပြင် sort() က array အသစ် မဆောက်ဘဲ မူရင်း array ကိုပဲ နေရာ ပြန်စီပြီး အဲဒီ array ကိုပဲ ပြန်ပေးပါတယ်။ ဒါကြောင့် cheapestFirst နဲ့ prices က array တစ်ခုတည်း ဖြစ်နေပါတယ်။ prices.toSorted((a, b) => a - b) ဒါမှမဟုတ် [...prices].sort((a, b) => a - b) က ဒီပြဿနာ နှစ်ခုလုံးကို ဖြေရှင်းပါတယ်။
Core concept: index, mutation နဲ့ callback method တွေ
Array က zero-indexed ဖြစ်တဲ့ အစဉ်လိုက် collection ပါ။ ပထမ item က arr[0]၊ နောက်ဆုံး item က arr[arr.length - 1] (ဒါမှမဟုတ် arr.at(-1)) ပါ။ မရှိတဲ့ index ကို ဖတ်ရင် error မတက်ဘဲ undefined ရပါတယ်။ Array က object တစ်မျိုးဖြစ်လို့ object lesson မှာ မြင်ခဲ့တဲ့ reference စည်းမျဉ်းအတိုင်း const နဲ့ ကြေညာထားလည်း item တွေကို ပြင်လို့ ရပြီး const b = a; က array အသစ် မဆောက်ပါဘူး။
Array method တွေကို အုပ်စု နှစ်စု ခွဲမှတ်ပါ။ ပထမအုပ်စုက မူရင်း array ကို တိုက်ရိုက် ပြောင်းပါတယ် (mutate)။ ဒုတိယအုပ်စုက မူရင်းကို မထိဘဲ array အသစ် ဒါမှမဟုတ် value အသစ် ပြန်ပေးပါတယ်။ ဘယ်အုပ်စုလဲ မသိဘဲ သုံးရင် problem section လို bug တွေ ဖြစ်ပါတယ်။
| Method | မူရင်းကို ပြောင်းလား | ပြန်ပေးတာ |
|---|---|---|
| push / pop | ပြောင်းတယ် | length အသစ် / ဖယ်လိုက်တဲ့ item |
| splice / sort / reverse | ပြောင်းတယ် | ဖယ်လိုက်တဲ့ item / array ကိုယ်တိုင် |
| map / filter / slice / concat | မပြောင်းဘူး | array အသစ် |
| find / some / every / reduce | မပြောင်းဘူး | item, boolean ဒါမှမဟုတ် စုပေါင်း value |
map, filter, forEach လို method တွေက callback function တစ်ခုကို item တိုင်းအတွက် ခေါ်ပြီး (item, index, array) ကို ပို့ပေးပါတယ်။ map က callback ရဲ့ return value တွေနဲ့ အရှည်တူ array အသစ် ဆောက်ပြီး filter က true ပြန်တဲ့ item တွေကိုပဲ ထားပါတယ်။ forEach ကတော့ ဘာမှ မဆောက်ဘဲ undefined ပြန်လို့ print ထုတ်တာလို side effect တွေအတွက်ပဲ သုံးပါ။ find က ပထမဆုံး ကိုက်တဲ့ item ကို (မတွေ့ရင် undefined) ပြန်ပေးပြီး some/every က boolean ပြန်ပေးပါတယ်။ reduce ကတော့ item အားလုံးကို value တစ်ခုတည်း (စုစုပေါင်း စသဖြင့်) အဖြစ် စုပေါင်းပြီး initial value ကို အမြဲ ပေးပါ။
sort() ကို number တွေအတွက် သုံးရင် (a, b) => a - b လို comparator ကို အမြဲ ပေးပါ။ Comparator က အနုတ် ပြန်ရင် a ကို ရှေ့မှာ ထားပြီး အပေါင်း ပြန်ရင် b ကို ရှေ့မှာ ထားပါတယ်။ မူရင်းကို မပြောင်းချင်ရင် Node 20+ နဲ့ ခေတ်ပေါ် browser တွေမှာ ပါတဲ့ toSorted() ကို သုံးပါ။
- Method ကို ရွေးမယ့်အခါ မေးပါ
- မူရင်းကို ပြောင်းလား (push, sort)
- Array အသစ် ပြန်လား (map, filter)
- ရလဒ်ကို variable အသစ်ထဲ သိမ်း
Array ကို တခြား code နဲ့ မျှသုံးနေရင် array အသစ် ပြန်ပေးတဲ့ method တွေကို ဦးစားပေးပါ။
အထွေထွေ ပုံစံ
const list = [itemA, itemB, itemC];
list[index]; // read one item
list.push(newItem); // add to the end (mutates)
const changed = list.map(function (item) {
return transform(item); // new array, same length
});
const kept = list.filter(function (item) {
return condition(item); // new array, matching items only
});Index နဲ့ ဖတ်တာ၊ push နဲ့ အဆုံးမှာ ထည့်တာ၊ map နဲ့ item တိုင်းကို ပြောင်းတာ၊ filter နဲ့ ရွေးထုတ်တာ ဆိုတဲ့ အခြေခံ ပုံစံ လေးခုပါ။ map နဲ့ filter ရဲ့ callback က return ပြန်ရပါမယ်။
Example: fruits array ကို ထည့်၊ ဖြတ်၊ ပြောင်း
fruits array ထဲကို push နဲ့ item တစ်ခု ထည့်ပြီး forEach နဲ့ item တိုင်းကို print ထုတ်ပါတယ်။ ပြီးရင် map နဲ့ စာလုံးကြီး array အသစ် ဆောက်ပြီး မူရင်း array မပြောင်းကြောင်း စစ်ပါတယ်။
const fruits = ["Banana", "Orange", "Apple"];
// Add a new item to the end
fruits.push("Mango");
// Loop through the array
fruits.forEach(function(fruit) {
console.log("I like " + fruit);
});
const loud = fruits.map(function(fruit) {
return fruit.toUpperCase();
});
console.log(loud);
console.log(fruits.length + " fruits, first is " + fruits[0]);Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Line 4
fruits.push("Mango");push က မူရင်း array ရဲ့ အဆုံးမှာ item ထည့်ပါတယ် (mutate)။ fruits ကို const နဲ့ ကြေညာထားပေမယ့် ရပါတယ်၊ variable ကို ပြန် assign လုပ်တာ မဟုတ်လို့ပါ။
- Lines 7-9
fruits.forEach(function(fruit) {forEach က item တစ်ခုစီအတွက် callback ကို ခေါ်ပြီး fruit ထဲမှာ လက်ရှိ item ကို ထည့်ပေးပါတယ်။ Print ထုတ်တာက side effect ဖြစ်လို့ forEach နဲ့ ကိုက်ပါတယ်။
- Lines 11-13
return fruit.toUpperCase();map ရဲ့ callback က return ပြန်တဲ့ value တွေနဲ့ array အသစ် ဆောက်ပါတယ်။ return ကို မေ့ရင် loud က [undefined, undefined, ...] ဖြစ်ပါမယ်။
- Line 15
console.log(fruits.length + " fruits, first is " + fruits[0]);push ကြောင့် length က 4 ဖြစ်ပြီး fruits[0] က "Banana" (စာလုံးသေး) ဖြစ်နေဆဲပါ — map က မူရင်းကို မပြောင်းခဲ့ပါဘူး။
I like Banana
I like Orange
I like Apple
I like Mango
[ 'BANANA', 'ORANGE', 'APPLE', 'MANGO' ]
4 fruits, first is Bananaရှာ၊ စစ်၊ စုပေါင်း: find, some, every, reduce
Score array တစ်ခုပေါ်မှာ method တစ်ခုချင်းကို run ကြည့်ပါ။ find က ပထမဆုံး ကိုက်တဲ့ item တစ်ခုကိုပဲ ပြန်ပြီး filter က ကိုက်တဲ့ item အားလုံးကို array အဖြစ် ပြန်ပေးပါတယ်။ reduce ရဲ့ ဒုတိယ argument 0 က initial value ပါ — array ဗလာ ဖြစ်နေလည်း 0 ပြန်ပြီး TypeError မတက်ပါဘူး။ နောက်ဆုံး line က comparator နဲ့ toSorted ကို သုံးပြီး မူရင်း မပြောင်းကြောင်း ပြပါတယ်။
const scores = [72, 95, 58, 88];
const firstFail = scores.find(function (s) { return s < 60; });
const passed = scores.filter(function (s) { return s >= 60; });
const anyPerfect = scores.some(function (s) { return s === 100; });
const allPositive = scores.every(function (s) { return s > 0; });
const total = scores.reduce(function (sum, s) { return sum + s; }, 0);
const emptyTotal = [].reduce(function (sum, s) { return sum + s; }, 0);
console.log(firstFail, passed, anyPerfect, allPositive, total, emptyTotal);
console.log(scores.toSorted(function (a, b) { return a - b; }));
console.log(scores); // still in the original orderလက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
Online shop တစ်ခုရဲ့ product list page က array method တွေကို ဆက်တိုက် သုံးတဲ့ ဥပမာ ကောင်းပါတယ်။ API ကနေ product object array ရလာတာနဲ့ filter နဲ့ stock ရှိတာတွေကို ရွေး၊ map နဲ့ "Notebook - $2.50" လို display text ပြောင်း၊ reduce နဲ့ cart ရဲ့ စုစုပေါင်း ဈေးနှုန်းကို တွက်ပါတယ်။ Search box က includes ဒါမှမဟုတ် filter ကို သုံးပြီး "Sort by price" ခလုတ်က comparator နဲ့ sort လုပ်ပါတယ်။
အရေးကြီးတာက API ကနေ ရလာတဲ့ မူရင်း array ကို မပြောင်းဖို့ပါ — user က filter ကို ဖြုတ်လိုက်ရင် product အားလုံးကို ပြန်ပြရမှာမို့လို့ပါ။ ဒါကြောင့် project အများစုက filter/map/toSorted လို array အသစ် ပြန်ပေးတဲ့ method တွေကို ဦးစားပေးပြီး test ထဲမှာ "ပြီးတဲ့နောက် မူရင်း array ရဲ့ length နဲ့ အစဉ် မပြောင်း" ဆိုတာကို စစ်ပါတယ်။ React လို UI library တွေကလည်း state array ကို push နဲ့ ပြင်ရင် ပြောင်းလဲမှုကို မသိနိုင်လို့ array အသစ် ပေးဖို့ လိုပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. Number တွေကို comparator မပါဘဲ sort လုပ်တာ
[10, 9, 100, 1].sort() က [1, 10, 100, 9] ပြန်ပြီး မူရင်းကိုပါ ပြောင်းပါတယ်။ Number တွေအတွက် (a, b) => a - b ကို အမြဲ ပေးပြီး မူရင်း လိုသေးရင် toSorted() ကို သုံးပါ။
2. forEach ကနေ ရလဒ် မျှော်လင့်တာ
const doubled = nums.forEach(function (n) { return n * 2; }); ဆိုရင် doubled က undefined ပါ၊ forEach က callback ရဲ့ return value တွေကို လွှင့်ပစ်လို့ပါ။ Array အသစ် လိုရင် map ကို သုံးပါ။
3. Array ဗလာပေါ်မှာ initial value မပါတဲ့ reduce
[].reduce(function (a, b) { return a + b; }) က "Reduce of empty array with no initial value" TypeError တက်ပါတယ်။ Filter လုပ်ပြီးနောက် array ဗလာ ဖြစ်နိုင်လို့ reduce(..., 0) လို initial value ကို အမြဲ ပေးပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Array variable တွေကို products, scores လို အများကိန်း နာမည်ပေးပြီး callback parameter ကို product, score လို အနည်းကိန်း နာမည်ပေးပါ။
- filter → map → reduce လို အဆင့်တွေကို ခွဲရေးပြီး အဆင့်တစ်ခုစီရဲ့ ရလဒ်ကို နာမည်ပါတဲ့ variable ထဲ ထားပါ၊ debug လုပ်ရ လွယ်ပါတယ်။
- Item တစ်ခုတည်း လိုရင် filter(...)[0] အစား find ကို သုံးပါ၊ ကိုက်တာ တွေ့တာနဲ့ ရပ်လို့ ပိုမြန်ပါတယ်။
Mini challenge
Mini challenge: stock ရှိတဲ့ product catalog
Product object array တစ်ခုကနေ stock ရှိတာတွေကို ရွေး၊ display label ပြောင်း၊ စုစုပေါင်း ဈေးနှုန်း တွက်ပြီး မူရင်း array မပြောင်းကြောင်း စစ်တဲ့ buildCatalog() ကို ရေးပါ။
- filter နဲ့ stock > 0 ဖြစ်တဲ့ product တွေကိုပဲ ရွေးရမယ်
- map နဲ့ "Name - $2.50" ပုံစံ label array ဆောက်ရမယ်
- reduce (initial value 0) နဲ့ ရွေးထားတဲ့ product တစ်ခုစီ တစ်ခုစီရဲ့ စုစုပေါင်း ဈေးနှုန်း တွက်ရမယ်
- find နဲ့ $2 အောက် ပထမဆုံး in-stock product ကို ရှာပြီး some နဲ့ ကုန်သွားတဲ့ product ရှိမရှိ စစ်ရမယ်
- အဆုံးမှာ မူရင်း array မှာ product 4 ခု ရှိနေဆဲ ဖြစ်ကြောင်း print ထုတ်ရမယ်
const products = [
{ name: "Notebook", price: 2.5, stock: 12 },
{ name: "Pen", price: 0.8, stock: 0 },
{ name: "Backpack", price: 24, stock: 3 },
{ name: "Ruler", price: 1.2, stock: 5 },
];
function buildCatalog(list) {
// TODO: available = only products with stock > 0 (filter)
// TODO: labels = "Name - $price" with 2 decimals (map)
// TODO: total = sum of available prices, starting from 0 (reduce)
// TODO: return { labels, total }
}
// TODO: print the labels and the total
// TODO: find the first in-stock product under $2; check if any product is sold out
// TODO: print how many products the source array still hasရလဒ်: Output က ဒီလို ဖြစ်ရပါမယ်: [ 'Notebook - $2.50', 'Backpack - $24.00', 'Ruler - $1.20' ] Total of one each: $27.70 Cheap pick: Ruler Any sold out? true Source still has 4 products
အရိပ်အမြွက် ကြည့်ရန်
map ထဲမှာ p.price.toFixed(2) က 2.5 ကို "2.50" ပြောင်းပေးပါတယ်။ find ရဲ့ callback မှာ p.stock > 0 && p.price < 2 ဆိုတဲ့ condition နှစ်ခုလုံး ပါရပါမယ်။
အဖြေ ကြည့်ရန်
const products = [
{ name: "Notebook", price: 2.5, stock: 12 },
{ name: "Pen", price: 0.8, stock: 0 },
{ name: "Backpack", price: 24, stock: 3 },
{ name: "Ruler", price: 1.2, stock: 5 },
];
function buildCatalog(list) {
const available = list.filter(function (p) {
return p.stock > 0;
});
const labels = available.map(function (p) {
return p.name + " - $" + p.price.toFixed(2);
});
const total = available.reduce(function (sum, p) {
return sum + p.price;
}, 0);
return { labels, total };
}
const catalog = buildCatalog(products);
console.log(catalog.labels);
console.log("Total of one each: $" + catalog.total.toFixed(2));
const cheap = products.find(function (p) {
return p.stock > 0 && p.price < 2;
});
const anySoldOut = products.some(function (p) {
return p.stock === 0;
});
console.log("Cheap pick: " + cheap.name);
console.log("Any sold out? " + anySoldOut);
console.log("Source still has " + products.length + " products");Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Array က 0 ကနေ စတဲ့ index နဲ့ အစဉ်လိုက် collection ဖြစ်ပြီး နောက်ဆုံး item က arr[arr.length - 1] ပါ။
- push, pop, splice, sort, reverse က မူရင်းကို ပြောင်းပြီး map, filter, slice, concat က array အသစ် ပြန်ပေးပါတယ်။
- forEach က side effect အတွက်ပဲ (undefined ပြန်) ဖြစ်ပြီး ပြောင်းထားတဲ့ array လိုရင် map ကို သုံးပါ။
- find, some, every, reduce က ရှာ၊ စစ်၊ စုပေါင်းပေးပြီး reduce ကို initial value အမြဲ ပေးပါ။
- Number တွေကို comparator (a, b) => a - b နဲ့ sort လုပ်ပြီး မူရင်း လိုရင် toSorted() ကို သုံးပါ။
MDN — JavaScript Guide — MDN Web Docs