1.filter
filter函數的主要用途是對數組元素進行過濾,並返回一個符合條件的元素的數組
let nums = [10,20,30,111,222,333]
選出nums中小於100的數:
let newNums = nums.filter(n => n<100)
2.map
map函數是對數組每個元素的映射操作,並返回一個新數組,原數組不會改變
將newNums中每個數字乘2
let new2Nums = newNums.map(n => n*2)
3.reduce
reduce函數主要用於對數組所以元素的匯總操作,如全部相加、相乘等
將new2Nums中數字全部相加:
let new3Nums = new2Nums.reduce((preValue,n) =>preValue + n)
preValue為前一次累加的和