- Published on
JS30 - 04 Array Cardio 1
這篇筆記記錄如何使用 JavaScript Array 的操作方法,包括 filter
、map
、sort
、reduce
,並且透過練習來熟悉這些方法的使用
filter
:篩選符合條件的元素map
:對陣列元素進行轉換,並回傳新陣列sort
:排序陣列中的元素reduce
:累加陣列中的元素,並回傳單一值
📌 filter:篩選符合條件的元素
filter()
方法會將所有陣列中的元素分別傳入 callback 函式,並將所有回傳值為 true 的元素建構成新的陣列,此方法不會改變原陣列
// 篩選 1500 年到 1600 年之間的發明家
const filteredInventors = inventors.filter((inventor) => inventor.year >= 1500 && inventor.year < 1600);
📌 map:對陣列元素進行轉換,並回傳新陣列
map()
方法會建立一個新的陣列,其內容為原陣列的每一個元素經由 callback 函式運算後所回傳的結果之集合
// 取得發明家的全名
const fullNames = inventors.map((inventor) => `${inventor.first} ${inventor.last}`);
📌 sort:排序陣列中的元素
sort()
方法會原地對一個陣列的所有元素進行排序,並回傳排序後的陣列。預設排序是根據字串的 Unicode 編碼順序
// 依照發明家的出生年份排序
const sortedInventors = inventors.sort((a, b) => a.year - b.year);
比較數字時,可以使用 a - b
來排序
const numbers = [5, 2, 10, 1];
const sortedNumbers = numbers.sort((a, b) => a - b);
console.log(sortedNumbers); // [1, 2, 5, 10]
- 若
a - b
結果為負數,則a
排在b
前面 - 若
a - b
結果為正數,則b
排在a
前面 - 若
a - b
結果為 0,則a
與b
相等
若要依照字串排序,可以使用 localeCompare()
方法
// 依照發明家的姓氏排序
const sortedInventors = inventors.sort((a, b) => a.last.localeCompare(b.last));
localeCompare()
方法會比較兩個字串,並回傳一個數字- 回傳值為負數,表示
a
排在b
前面 - 回傳值為正數,表示
b
排在a
前面 - 回傳值為 0,表示
a
與b
相等
- 回傳值為負數,表示
📌 reduce:累加陣列中的元素,並回傳單一值
reduce()
方法將一個累加器及陣列中每項元素(由左至右)傳入 callback 函式,將陣列化為單一值
// 計算所有發明家的年齡總和
const totalYears = inventors.reduce((total, inventor) => total + (inventor.passed - inventor.year), 0);
reduce()
方法的第二個參數是累加器的初始值,這裡設定為 0total
是累加器,初始值為 0- 如果沒有設定初始值,則陣列第一個元素會成為初始值,並從第二個元素開始執行 callback 函式
簡易數字累加範例:
const numbers = [1, 2, 3, 4, 5];
const total = numbers.reduce((total, number) => total + number, 0);
console.log(total); // 15
拆解過程:
0 + 1 = 1
1 + 2 = 3
3 + 3 = 6
6 + 4 = 10
10 + 5 = 15